Strings (Part 2)

The types of substitution can be easily seen such as; strings inside a single quote have two consecutive backslashes interpreted as one, a backslash followed by a single quote is interpreted as a single quote and so on and so forth as shown below.

‘escape using “\\”‘ >> escape using “\”
‘That\’s right’ >> That’s right

Stings that are double-quoted have the ability to support more escape sequences, such as “\n” which denotes a new line character. You can search the web for the full list of these escape sequences. Any Ruby expression can be substituted as a string using the #{expr} sequence. If that expression just happens to be a global variable, class variable or an instance variable, then the braces can be omitted.

“Seconds/day: #{24*60*60}” >> Seconds/day: 86400
“#{‘Happy!’*2}Birthday” >> Happy! Happy! Birthday
“This is line #$.” >> This is line 3

This entry was posted in Development, Programming Basics and tagged , . Bookmark the permalink. Comments are closed, but you can leave a trackback: Trackback URL.