Ruby supports string interpolation in which we can embed the expressions within the string literals. This is a very useful feature to create formatted strings in a more readable form. We are going to discuss string interpolation in Ruby, in this article.
A special sequence of characters “#{ }” we use to represent the expressions within the string literal. The expression should be embedded within the curly braces. For example:
pi = 3.14159265359 puts "The value of the PI is #{pi}."
The output of the above statement looks like the below:
The value of the PI is 3.14159265359.
Note that, string interpolation in Ruby works with only double-quoted strings, not with single-quoted strings. We can use arithmetic expressions, multi-line statements, and even function calls with the interpolated string.
| Nick |