Learn Ruby
String
3.29
Lesson

What is escape sequence

Sometimes in programming we need to convey non printable items. For example on laptops if you press wrong button then you hear a beep sound. How do we tell computer to beep.

Researches agreed that \a would be treated as an alarm or a beep. Notice that it starts with a \. Because we are running Ruby online you won’t be able to hear beep but if run \a on your laptop after installing Ruby then you will be able to hear the beep.

These special instructions that start with a backslash are called Escape Sequence.

Below is the list of Escape sequences with corresponding meaning of it.

` Single quote
" Double quote
\a Audible bell
\b Backspace
\f Form feed
\r Carriage return
\s A space
\t Horizontal tab
\n New line

Let’s try out the last one.

When we use escape sequence in single quote then Ruby treats it as a string and nothing special happens.

In the above case nothing special happened and \n is printed as it is. If the string is in double quote then Ruby obeys the rules of escape sequence.

In this case because of \n we get a new line.