The proper way to use string interpolation in Ruby is as follows:
name = "Ned Stark"
puts "Hello there, #{name}" #=> "Hello there, Ned Stark"
That is the way I intend to always use it.
However, I've noticed something odd in Ruby's string interpolation. I've noticed that string interpolation works in Ruby without the curly braces in regards to instance variables. For example:
@name = "Ned Stark"
puts "Hello there, #@name" #=> "Hello there, Ned Stark"
And that trying the same thing as a non-instance variable does not work.
name = "Ned Stark"
puts "Hello, there, #name" #=> "Hello there, #name"
I've tried this with success in both 1.9.2 and 1.8.7.
Why does this work? What is the interpreter doing here?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…