Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Escape Sequences3:54 with Jay McGavren
Sometimes, we need characters in a string that are hard to represent in program code.
Escape sequences are used to represent characters that would normally be difficult to include in program code.
skips to a new line
Inserts "double quotes"
Inserts 'single quotes'
Inserts a \ backslash
Here's some code that uses escape sequences:
puts "first line\nsecond line" puts "\tindented text" puts "He said, \"Whoa.\""
first line second line indented text He said, "Whoa."
You don't always have to escape quotation marks in your strings:
puts "Double-quoted strings can hold single quotes: ''" puts 'Single-quoted strings can hold double quotes: ""' # But... puts "Double-quotes in double-quoted strings need to be \"escaped\"." puts 'Single-quotes in single-quoted strings need to be \'escaped\'.'
We've created a workshop where you can get additional practice with Ruby strings. Don't miss this chance to strengthen your skills!
Visit the workshop here: Practice Strings in Ruby
Sometimes, we need characters in a string that are hard to 0:00 represent in program code. 0:02 The first line of code prints a string that includes a newline character. 0:05 But it's awkward to read, 0:08 with the second half of the string starting on the second line. 0:10 We want line 3 to include a tab character to indent some text, but 0:14 when I press the tab key, my editor inserted space characters, instead. 0:18 And line 4 isn't even valid Ruby code. 0:22 It's supposed to be a single string that contains double quote marks. 0:25 But what we wind up with is a string containing he said, some invalid Ruby code 0:28 referring to a non-existent object named Whoa, and a second string, which is empty. 0:34 To address this problem, Ruby offers escape sequences. 0:41 An escape sequence is a portion of a string that represents a character that 0:46 would normally be hard to represent in program code. 0:50 Each of these two character combinations you see here 0:53 actually represents a single character. 0:56 So \ followed by n, represents a new line character, which skips to a new line. 0:59 \ followed by t, represents a tab character, which indents text. 1:05 \ followed by a " character inserts double quotes, even into a double quoted string. 1:10 \ followed by a ' character inserts single quotes, 1:16 again, even into a single quoted string. 1:20 And because you're going to need a way to insert backslash characters without Ruby 1:22 thinking you're starting an escape sequence, 1:26 \\ inserts a single backslash character. 1:28 Let's update the previous program to use escape sequences for 1:33 the hard to represent characters. 1:37 So first, let's take the new line character that's in the middle of this 1:39 first line and replace it with \n to represent an actual new line character. 1:42 Let's take the two spaces here on the second line and replace it 1:48 with \t which represents a tab character, that should indent the text for us. 1:51 And let's take the double quote characters that Ruby is mistaking for 1:57 the end of a string, and escape those using backslashes as well. 2:00 So that all of this becomes a single string. 2:04 Let's save that, and try rerunning it. 2:07 And everything works. 2:12 Our \n here on the first line, 2:13 gets replaced with a new line character down below. 2:16 \t gets replaced with a tab character. 2:18 And our \" get replaced with actual quotation marks. 2:22 Just so you know, you don't always have to escape quotation marks into your strings. 2:28 Double quoted strings can hold single quotes without escaping them. 2:33 Single quoted strings can hold double quotes without escaping them. 2:36 But, double quote characters within double quoted strings need to be escaped. 2:40 And so do single quotes within single quoted strings. 2:44 So, now that we know about escape sequences, 2:48 we know that the \n at the end of our answer variable is a new line character. 2:50 When the user presses the enter key to signal they're finished entering 2:56 an answer, it adds a new line character on the end of the string, 2:59 which gets returned by gets. 3:03 To remove it, we'll need to call a method on the string object, 3:06 which we'll learn how to do in the next stage. 3:09 Knowing how to work with strings is another essential programming skill, 3:12 one that's definitely worth practicing. 3:15 Be sure to check the teacher's notes to learn ways to get more practice with 3:17 strings. 3:21 Now you've learned the basics of working with strings in Ruby. 3:22 You can append strings to other strings by concatenating them. 3:25 You can embed data within strings using interpolation. 3:29 And you can embed special characters using escape sequences. 3:32 But that's only a small fraction of the features Ruby offers for 3:37 working with strings. 3:40 To do more, you're going to need to know how to call methods directly on 3:41 the strings, and not just for strings. 3:45 Virtually everything in Ruby has methods you can call on it. 3:48 We'll learn how to do that in the next stage. 3:51
You need to sign up for Treehouse in order to download course files.Sign up