Ruby Programming/Strings

Like Python, Java, and the .NET Framework, Ruby has a built-in String class.

String literals
One way to create a String is to use single or double quotes inside a Ruby program to create what is called a string literal. We've already done this with our "hello world" program. A quick update to our code shows the use of both single and double quotes.

Being able to use either single or double quotes is similar to Perl, but different from languages such as C and Java, which use double quotes for string literals and single quotes for single characters.

So what difference is there between single quotes and double quotes in Ruby? In the above code, there's no difference. However, consider the following code:

Because " " contains an apostrophe, which is the same character as the single quote, in the second line we need to use a backslash to escape the apostrophe so that Ruby understands that the apostrophe is in the string literal instead of marking the end of the string literal. The backslash followed by the single quote is called an escape sequence.

Single quotes
Single quotes only support two escape sequences.


 * \' – single quote
 * \\ – single backslash

Except for these two escape sequences, everything else between single quotes is treated literally.

Double quotes
Double quotes allow for many more escape sequences than single quotes. They also allow you to embed variables or Ruby code inside of a string literal – this is commonly referred to as interpolation.

Escape sequences
Below are some of the more common escape sequences that can appear inside of double quotes.


 * \" – double quote
 * \\ – single backslash
 * \a – bell/alert
 * \b – backspace
 * \r – carriage return
 * \n – newline
 * <tt>\s </tt>– space
 * <tt>\t </tt>– tab

Try out this example code to better understand escape sequences.

The result:

$ double-quotes.rb Hello&#09;&#09;world Goodbye world Start over world 1. Hello 2. World

Notice that the newline escape sequence (in the last line of code) simply starts a new line.

The bell character, produced by escape code, is considered a control character. It does not represent a letter of the alphabet, a punctuation mark, or any other written symbol. Instead, it instructs the terminal emulator (called a console on Microsoft Windows) to "alert" the user. It is up to the terminal emulator to determine the specifics of how to respond, although a beep is fairly standard. Some terminal emulators will flash briefly.

Run the following Ruby code to check out how your terminal emulator handles the bell character.

puts
We've been using the  function quite a bit to print out text. Whenever  prints out text, it automatically prints out a newline after the text. For example, try the following code.

The result:

$ hello-world.rb Say hello to the world

print
In contrast, Ruby's  function only prints out a newline if you specify one. For example, try out the following code. We include a newline at the end of 's argument list so that the shell prompt appears on a new line, after the text.

The result:

$ hello-world.rb Sayhellototheworld

The following code produces the same output, with all the words run together.