Why does string interpolation work in Ruby when there are no curly braces?

According to The Ruby Programming Language by Flanagan and Matsumoto:

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:

@var = "Hi"
puts "#@var there!"  #=> "Hi there!"

@@var = "Hi"
puts "#@@var there!" #=> "Hi there!"

$var = "Hi"
puts "#$var there!"  #=> "Hi there!"

Leave a Comment