Kaleem Ullah's Blog

Ruby String Interpolation

Posted in Ruby by Kaleem Ullah on January 10, 2011

The string interpolation in ruby is defined as ‘evaluation of any expression(and conversion to a string) in another string’. Like

str = "String";

"This is #{str} interpolation" => This is String interpolation

As you noted above, # and {} needs to evaluate expression in a string.

Only in double quotes string literals  an expression can be evaluated.

If there is any global, instance or any class variable, then there is no need of curly braces {}.

For Example

@hrs = 24

"There are #@hrs hours in a day."   => There are 24 hours in a day.

Enjoy!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: