CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Ruby Programming – String interpolation

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 |

Ruby Programming – String interpolation

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top