Tuesday, August 6, 2013

Interpolating Instance, Class level or Global variable in Ruby

When the expression to be interpolated into the string literal is simply a reference to a global, instance or class variable, then the curly braces may be omitted.

So the following should all work: 

@first_name = "Anil"
puts "#@first_name Galve"  #=> "Anil Galve"

@@first_name = "Anil"
puts "#@@first_name Galve" #=> "Anil Galve"

$first_name = "Anil"
puts "#$first_name Galve"  #=> "Anil Galve"

No comments:

Post a Comment