Ruby Programming/Strings

← Hello world | Alternate quotes →


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

String literals

edit

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 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:

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 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

edit

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

edit

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.

puts "Enter name"
name = gets.chomp
puts "Your name is #{name}"

Escape sequences

edit

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.rb
Hello		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 \a, 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 "\aHello world\a"

puts

edit

We've been using the puts function quite a bit to print out text. Whenever puts 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.rb
Say
hello
to
the
world

print

edit

In contrast, Ruby's print 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 print'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.rb
Sayhellototheworld

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"

See also

edit

String literals