1. Computing

String Literals

By

String objects hold ordered sequences of bytes, typically characters, usually to form pieces of human-readable text. They're a very common object type in all programming languages, and Ruby has a number of high-level and a few low-level ways to create, access and manipulate String objects.

Strings are most often created with a String literal. A literal is a special syntax in the Ruby language that creates an object of a specific type. For example, 23 is a literal that creates a Fixnum object. As for String literals, there are several forms.

Single-Quotes and Double-Quoted Strings

Most languages have a String literal similar to this, so this may be familiar. The type types of quotes, ' (single quote, apostrophe or hard quote) and " (double quote or soft quote) are used to enclose string literals, anything between them will be turned into String objects. The following example demonstrates this.


str1 = "Hello, Ruby world!"
str2 = 'Single quotes work too.'

But there are some differences between single and double quotes. Double quotes, or soft quotes enable some magic to happen behind the scenes. Most useful is interpolation inside strings, useful for inserting the value of a variable into the middle of a string. This is achieved by using the #{ … } sequence. The following example will ask you for your name and greet you, using interpolation to insert your name into the string literal that's printed.


print "What is your name? "
name = gets.chomp
puts "Hello, #{name}"

Note that any code can go inside the braces, not just variable names. Ruby will evaluate that code and whatever is returned it will attempt to insert it into the string. So you could just as easily say "Hello, #{gets.chomp}" and forget about the name variable. However, it's good practice not to put long expressions inside the braces.

Single quotes, apostrophes or hard quotes are much more restrictive. Inside of the single quotes, Ruby will perform no interpolation or escape sequences other than escaping the single quote character and backslash itself (\' and \\ respectively). If you don't intend to use interpolation, it's recommended to use single quotes more often than not.

The following example will attempt to interpolate a variable inside of single quotes.


print 'What is your name? '
name = gets.chomp
puts 'Hello, #{name}'

If you run this you'll get no error, but what will be printed?


$ ruby single-quote.rb
What is your name? Michael
Hello, #{name}
$

The interpolation sequence was passed through un-interpreted.

When Should I Use Single and Double Quotes

This is a matter of style. Some prefer to use double quotes all of the time unless they become inconvenient. Others would rather use single quotes unless the interpolation behavior is intended. There's nothing inherently dangerous about using double quotes all of the time, but it does make some code easier to read. You don't need to read a string when reading through code if you know there are no interpolations in it, because you know the string itself won't have any side effects. So which string literal form you use is up to you, there is no real right and wrong way here.

Escape Sequences

What if, in a string literal, you want to include the a quote character? For instance, the string "Steve said "Moo!"" won't work. And neither will 'Can't touch this!'. Both of these string include the quote character inside of the string, effectively ending the string literal and causing a syntax error. You could switch quote characters, like 'Steve said "Moo!"', but that doesn't really solve the problem. Instead, you can escape any quote character inside the string, and it will lose its special meaning (in this case, the special meaning is to close the string).

To escape a character, prepend it with the backslash character. The backslash character tells Ruby to ignore any special meaning the next character may have. If it's a matching quote character, don't end the string. If it's a hash sign, don't start an interpolation block. The following example demonstrates this use of backslash to escape special characters.


puts "Steve said \"Moo!\""
puts "String interpolation like \#{this}"
puts 'Can\'t touch this!'
puts "Print a backslash like this \\"

The backslash character can be used to remove any special meaning from the following character but, confusingly, it can also be used to denote special behavior in double-quoted strings. Most of these special behaviors have to do with inserting characters and byte sequences that cannot be typed or represented visually. Not all Strings are character strings, or may contain control sequences intended for the terminal, and not the user. Ruby gives you the ability to insert these types of strings using the backslash escape character.

  • \n - A newline character. The puts method does this automatically, but if you wish to insert one in the middle of a string, or the string is destined for something other than the puts method, you can use this to insert a newline in a string.
  • \t - A tab character. The tab character moves the cursor over (on most terminals) to a multiple of 8, so this is very useful for display tabular data. However, there are better ways of doing this, and using the tab character is considered a bit archaic or hackish.
  • \nnn - A backslash followed by 3 numbers will denote an ASCII character represented by 3 octal digits. Why octal? Mostly for historic reasons.
  • \xnn - A backslash, an x and 2 hex digits. The same as the octal version, only with hex digits.

You'll probably never use most of these, but know that they exist. And also remember that they only work in double quoted strings.

The next page discusses multi-line strings and an alternate syntax for string literals.

  1. About.com
  2. Computing
  3. Ruby
  4. Beginning Ruby
  5. Strings
  6. String Literals

©2014 About.com. All rights reserved.