Strings in Ruby

Tutorial Difficulty Level    

Ruby has many ways of making strings, which are generally variations of the many ways Perl has of making strings. Double quotes allow values to be interpolated into the string, while single quotes do not. Most escapes are treated literally in single quotes, including the fact that \n is treated as two characters, a backward slash followed by an n.

Ruby strings may extend any number of lines. This can be useful, but make sure you don’t leave out any closing quotes.

The << notation follows Perl and other languages as a way to multi-line strings. Those don’t generally permit the other varieties to specify multi-line strings, so it’s actually kind of redundant in Ruby.

The % can be used to create strings using a different delimiter. %Qx starts a double-quote style string which ends with the next x, rather than the usual double quote character. %qy starts a single-quote string.