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
represent in program code.
The first line of code prints a string that includes a newline character.
But it's awkward to read,
with the second half of the string starting on the second line.
We want line 3 to include a tab character to indent some text, but
when I press the tab key, my editor inserted space characters, instead.
And line 4 isn't even valid Ruby code.
It's supposed to be a single string that contains double quote marks.
But what we wind up with is a string containing he said, some invalid Ruby code
referring to a non-existent object named Whoa, and a second string, which is empty.
To address this problem, Ruby offers escape sequences.
An escape sequence is a portion of a string that represents a character that
would normally be hard to represent in program code.
Each of these two character combinations you see here
actually represents a single character.
So \ followed by n, represents a new line character, which skips to a new line.
\ followed by t, represents a tab character, which indents text.
\ followed by a " character inserts double quotes, even into a double quoted string.
\ followed by a ' character inserts single quotes,
again, even into a single quoted string.
And because you're going to need a way to insert backslash characters without Ruby
thinking you're starting an escape sequence,
\\ inserts a single backslash character.
Let's update the previous program to use escape sequences for
the hard to represent characters.
So first, let's take the new line character that's in the middle of this
first line and replace it with \n to represent an actual new line character.
Let's take the two spaces here on the second line and replace it
with \t which represents a tab character, that should indent the text for us.
And let's take the double quote characters that Ruby is mistaking for
the end of a string, and escape those using backslashes as well.
So that all of this becomes a single string.
Let's save that, and try rerunning it.
And everything works.
Our \n here on the first line,
gets replaced with a new line character down below.
\t gets replaced with a tab character.
And our \" get replaced with actual quotation marks.
Just so you know, you don't always have to escape quotation marks into your strings.
Double quoted strings can hold single quotes without escaping them.
Single quoted strings can hold double quotes without escaping them.
But, double quote characters within double quoted strings need to be escaped.
And so do single quotes within single quoted strings.
So, now that we know about escape sequences,
we know that the \n at the end of our answer variable is a new line character.
When the user presses the enter key to signal they're finished entering
an answer, it adds a new line character on the end of the string,
which gets returned by gets.
To remove it, we'll need to call a method on the string object,
which we'll learn how to do in the next stage.
Knowing how to work with strings is another essential programming skill,
one that's definitely worth practicing.
Be sure to check the teacher's notes to learn ways to get more practice with
Now you've learned the basics of working with strings in Ruby.
You can append strings to other strings by concatenating them.
You can embed data within strings using interpolation.
And you can embed special characters using escape sequences.
But that's only a small fraction of the features Ruby offers for
working with strings.
To do more, you're going to need to know how to call methods directly on
the strings, and not just for strings.
Virtually everything in Ruby has methods you can call on it.
We'll learn how to do that in the next stage.
You need to sign up for Treehouse in order to download course files.Sign up