You can actually interpolate global, instance, and class variables omitting the braces:
$world = 'world'
puts "hello, #$world"
# hello, world
In your example both $fg
and @fg
are uninitialized and thus evaluated to nil
, that's why they are intorpolated as empty strings. When you write "abcd#fg"
nothing is interpolated because the #
is not followed by one of {
, @
, $
.
You can find the feature documented in the RubySpec (thanks to @DavidMiani).
If you ask me, don't rely on this behaviour and always interpolate variables using braces, both for readability and to avoid problems such:
@variable = 'foo'
puts "#@variable_bar"
This will output an empty string instead of the, probably, expected string "foo_bar
", because it is trying to interpolate the undefined instance variable @variable_bar
.