← Hello world| Alternate quotes →
LikePython,Java, and the.NET Framework, Ruby has a built-in String class.
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.
puts'Hello world'puts"Hello world"
Being able to use either single or double quotes is similar toPerl, but different from languages such asC andJava, 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:
puts"Betty's pie shop"puts'Betty\'s pie shop'
Because "Betty's" 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 isin the string literal instead of marking the end of the string literal. The backslash followed by the single quote is called anescape sequence.
Single quotes only support two escape sequences.
Except for these two escape sequences, everything else between single quotes is treated literally.
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 asinterpolation.
puts"Enter name"name=gets.chompputs"Your name is#{name}"
Below are some of the more common escape sequences that can appear inside of double quotes.
Try out this example code to better understand escape sequences.
puts"Hello\t\tworld"puts"Hello\b\b\b\b\bGoodbye world"puts"Hello\rStart over world"puts"1. Hello\n2. World"
The result:
$double-quotes.rbHello worldGoodbye worldStart over world1. Hello2. World
Notice that the newline escape sequence (in the last line of code) simply starts a new line.
Thebell character, produced by escape code\a, is considered acontrol character. It does not represent a letter of the alphabet, a punctuation mark, or any other written symbol. Instead, it instructs theterminal emulator (called aconsole onMicrosoft Windows) to "alert" the user. It is up to the terminal emulator to determine the specifics of how to respond, although abeep 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"\aHello world\a"
We've been using theputs function quite a bit to print out text. Wheneverputs prints out text, it automatically prints out a newline after the text. For example, try the following code.
puts"Say","hello","to","the","world"
The result:
$hello-world.rbSayhellototheworld
In contrast, Ruby'sprint function only prints out a newline if you specify one. For example, try out the following code. We include a newline at the end ofprint's argument list so that the shell prompt appears on a new line, after the text.
print"Say","hello","to","the","world","\n"
The result:
$hello-world.rbSayhellototheworld
The following code produces the same output, with all the words run together.
print"Say"print"hello"print"to"print"the"print"world"print"\n"