Interpolation is about using expressions and values inside a string. For example, consider a variable named a which contains 4 and variable b containing 6. Now, we have to print 4 * 6 = 24

a * b = a*b. Will this work?

Interpolation example in Ruby

Nope. It returned an unexpected result. We need the values of a, b and a*b to be printed. Now, Interpolation comes into picture. Using Interpolation, we can include expression and values inside strings.

To Interpolate the value of a, do the following: puts “ #{a} ”

Interpolation example in Ruby

It returned the value of a instead of "a" as a string. Likewise, we have to do the same for the variable b and the expression a*b.

Interpolation example in Ruby

Ruby evaluates the value of the variable or an expression given between the curly braces.


Ruby, evaluates the value of a*b and interpolates into string.

Interpolating Strings:

Consider a variable with a value.

person = “Sachin Tendulkar”;

Now, we have to display “I love Sachin Tendulkar”.

Interpolation example on Strings in Ruby

We’ve expected “I love Sachin Tendulkar” but it displayed “I love person”. Now, let’s interpolate the variable person.

Interpolation example on Strings in Ruby