...

/

String Interpolation

String Interpolation

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

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