String Interpolation

Learn about string interpolation in Ruby and why there’s a preference for it.

We'll cover the following

As mentioned before, we can stick strings together by using the + operator.

Consider the following code:

name = "Ada"
puts "Hello, " + name + "!"

This outputs the message Hello, Ada!.

Gluing strings together as this works. However, another method of accomplishing the same is widely used and usually preferred over concatenating strings with +. This method is called string interpolation, and this is how it looks:

Press + to interact
name = "Ada"
puts "Hello, #{name}!"

Using this syntax, everything between the opening #{ and closing } is evaluated as Ruby code, and the result of this evaluation is embedded into the string surrounding it.

In other words, when Ruby finds #{name} in this string, it evaluates the piece of Ruby code, name. It finds that this is a variable, so it returns the variable’s value, which is the "Ada" string. It embeds it into the surrounding "Hello, #{name}!" string by replacing #{name} with "Ada"

This reveals the difference between strings created with single and double quotes: string interpolation only works with double-quotes.

That means that:

Press + to interact
puts "Interpolation works in double quoted strings: #{1 + 2}."
puts 'And it does not work in single quoted strings: #{1 + 2}.'

Prints out:

Interpolation works in double-quoted strings: 3.
And it does not work in single-quoted strings: #{1 + 2}.

Why prefer string interpolation?

String interpolation, at the very least, involves less typing. It’s only a five-character difference in our example. Consider a longer string, however, which is constructed using three or more variables. This extra space quickly adds up, and things wouldn’t fit nicely on a single line anymore.

Many people also find that the syntax reads a bit better. There’s less clutter, making it easier to see what’s going on.

Another benefit of string interpolation over concatenation is that, while we can’t directly concatenate a string with a number using the + operator, we can embed a number inside a string using string interpolation.

For example, the following produces an error.

Press + to interact
Puts "I am the number " + 3

But this will do just fine:

Press + to interact
puts "I am the number #{3} "

In the background, the number 3 gets converted to a string before being embedded in another string.

Another small reason is that string interpolation uses fewer resources:

The code "Hello, #{name}!" creates one new string object and then embeds the existing "Ada" string into it.

The code "Hello, " + name + "!", on the other hand, creates three new string objects. It first creates the "!" string and then concatenates it with the existing "Ada" string. The + operator returns a new string, which is now "Ada!". This string is again concatenated with "Hello, ", which again creates a new string, "Hello, Ada!".

So, string concatenation creates two more string objects, even in our simple example. These intermediate objects are immediately discarded because they’re not used anymore. We’re only interested in the final result "Hello, Ada!".

We recommend getting used to using string interpolation because it’s what most developers use.