1 00:00:00,260 --> 00:00:02,990 Sometimes, we need characters in a string that are hard to 2 00:00:02,990 --> 00:00:05,310 represent in program code. 3 00:00:05,310 --> 00:00:08,933 The first line of code prints a string that includes a newline character. 4 00:00:08,933 --> 00:00:10,191 But it's awkward to read, 5 00:00:10,191 --> 00:00:13,100 with the second half of the string starting on the second line. 6 00:00:14,150 --> 00:00:18,010 We want line 3 to include a tab character to indent some text, but 7 00:00:18,010 --> 00:00:22,900 when I press the tab key, my editor inserted space characters, instead. 8 00:00:22,900 --> 00:00:25,690 And line 4 isn't even valid Ruby code. 9 00:00:25,690 --> 00:00:28,840 It's supposed to be a single string that contains double quote marks. 10 00:00:28,840 --> 00:00:34,550 But what we wind up with is a string containing he said, some invalid Ruby code 11 00:00:34,550 --> 00:00:40,570 referring to a non-existent object named Whoa, and a second string, which is empty. 12 00:00:41,960 --> 00:00:46,245 To address this problem, Ruby offers escape sequences. 13 00:00:46,245 --> 00:00:50,160 An escape sequence is a portion of a string that represents a character that 14 00:00:50,160 --> 00:00:53,820 would normally be hard to represent in program code. 15 00:00:53,820 --> 00:00:56,690 Each of these two character combinations you see here 16 00:00:56,690 --> 00:00:59,210 actually represents a single character. 17 00:00:59,210 --> 00:01:05,165 So \ followed by n, represents a new line character, which skips to a new line. 18 00:01:05,165 --> 00:01:10,115 \ followed by t, represents a tab character, which indents text. 19 00:01:10,115 --> 00:01:16,315 \ followed by a " character inserts double quotes, even into a double quoted string. 20 00:01:16,315 --> 00:01:20,090 \ followed by a ' character inserts single quotes, 21 00:01:20,090 --> 00:01:22,650 again, even into a single quoted string. 22 00:01:22,650 --> 00:01:26,780 And because you're going to need a way to insert backslash characters without Ruby 23 00:01:26,780 --> 00:01:28,895 thinking you're starting an escape sequence, 24 00:01:28,895 --> 00:01:33,690 \\ inserts a single backslash character. 25 00:01:33,690 --> 00:01:37,150 Let's update the previous program to use escape sequences for 26 00:01:37,150 --> 00:01:39,420 the hard to represent characters. 27 00:01:39,420 --> 00:01:42,640 So first, let's take the new line character that's in the middle of this 28 00:01:42,640 --> 00:01:48,160 first line and replace it with \n to represent an actual new line character. 29 00:01:48,160 --> 00:01:51,950 Let's take the two spaces here on the second line and replace it 30 00:01:51,950 --> 00:01:57,030 with \t which represents a tab character, that should indent the text for us. 31 00:01:57,030 --> 00:02:00,340 And let's take the double quote characters that Ruby is mistaking for 32 00:02:00,340 --> 00:02:04,290 the end of a string, and escape those using backslashes as well. 33 00:02:04,290 --> 00:02:07,440 So that all of this becomes a single string. 34 00:02:07,440 --> 00:02:08,804 Let's save that, and try rerunning it. 35 00:02:12,280 --> 00:02:13,762 And everything works. 36 00:02:13,762 --> 00:02:16,170 Our \n here on the first line, 37 00:02:16,170 --> 00:02:18,915 gets replaced with a new line character down below. 38 00:02:18,915 --> 00:02:22,400 \t gets replaced with a tab character. 39 00:02:22,400 --> 00:02:26,096 And our \" get replaced with actual quotation marks. 40 00:02:28,102 --> 00:02:33,140 Just so you know, you don't always have to escape quotation marks into your strings. 41 00:02:33,140 --> 00:02:36,690 Double quoted strings can hold single quotes without escaping them. 42 00:02:36,690 --> 00:02:40,150 Single quoted strings can hold double quotes without escaping them. 43 00:02:40,150 --> 00:02:44,410 But, double quote characters within double quoted strings need to be escaped. 44 00:02:44,410 --> 00:02:48,160 And so do single quotes within single quoted strings. 45 00:02:48,160 --> 00:02:50,650 So, now that we know about escape sequences, 46 00:02:50,650 --> 00:02:56,200 we know that the \n at the end of our answer variable is a new line character. 47 00:02:56,200 --> 00:02:59,710 When the user presses the enter key to signal they're finished entering 48 00:02:59,710 --> 00:03:03,560 an answer, it adds a new line character on the end of the string, 49 00:03:03,560 --> 00:03:06,200 which gets returned by gets. 50 00:03:06,200 --> 00:03:09,620 To remove it, we'll need to call a method on the string object, 51 00:03:09,620 --> 00:03:12,200 which we'll learn how to do in the next stage. 52 00:03:12,200 --> 00:03:15,770 Knowing how to work with strings is another essential programming skill, 53 00:03:15,770 --> 00:03:17,930 one that's definitely worth practicing. 54 00:03:17,930 --> 00:03:21,210 Be sure to check the teacher's notes to learn ways to get more practice with 55 00:03:21,210 --> 00:03:22,710 strings. 56 00:03:22,710 --> 00:03:25,910 Now you've learned the basics of working with strings in Ruby. 57 00:03:25,910 --> 00:03:29,400 You can append strings to other strings by concatenating them. 58 00:03:29,400 --> 00:03:32,970 You can embed data within strings using interpolation. 59 00:03:32,970 --> 00:03:37,120 And you can embed special characters using escape sequences. 60 00:03:37,120 --> 00:03:40,370 But that's only a small fraction of the features Ruby offers for 61 00:03:40,370 --> 00:03:41,960 working with strings. 62 00:03:41,960 --> 00:03:45,540 To do more, you're going to need to know how to call methods directly on 63 00:03:45,540 --> 00:03:48,310 the strings, and not just for strings. 64 00:03:48,310 --> 00:03:51,840 Virtually everything in Ruby has methods you can call on it. 65 00:03:51,840 --> 00:03:53,870 We'll learn how to do that in the next stage.