String Literals

Over shoulder close up of male office worker looking at laptop in office
Cultura RM Exclusive/Stefano Gilera / Getty Images

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

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.

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.

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

The interpolation sequence was passed through uninterpreted.

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 a quote character? For instance, the string "Steve said "Moo!" won't work. And neither will 'Can't touch this!'. Both of these strings 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.

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

Multi-Line Strings

Most languages don't allow multi-line string literals, but Ruby does. There's no need to end your strings and append more strings for the next line, Ruby handles multi-line string literals just fine with the default syntax.

Alternate Syntax

As with most other literals, Ruby provides an alternate syntax for string literals. If you're using a lot of quote characters inside your literals, for example, you may want to use this syntax. When you use this syntax is a matter of style, they're usually not needed for strings.

To use the alternate syntax, use the following sequence for single-quoted strings %q{ … }. Similarly, use the following syntax for double-quoted strings %Q{ … }. This alternate syntax follows all the same rules as their "normal" cousins. Also, note that you can use any characters you wish instead of braces. If you use a brace, square bracket, angle bracket or parenthesis, then the matching character will end the literal. If you don't want to use matching characters, you can use any other symbol (anything not a letter or number). The literal will be closed with another of the same symbol. The following example shows you several ways to use this syntax.

The alternate syntax also works as a multi-line string.

mla apa chicago
Your Citation
Morin, Michael. "String Literals." ThoughtCo, Aug. 28, 2020, Morin, Michael. (2020, August 28). String Literals. Retrieved from Morin, Michael. "String Literals." ThoughtCo. (accessed June 7, 2023).