I saw this example in an answer elsewhere and with the following it outputs foobar
:
a = :foo
def bar(b)
:"#{b}bar"
end
c = bar(a)
c
The colon isn't an operator inside bar
, it is simply a Symbol
literal that uses string interpolation to build the Symbol
. Some Symbol
s need to be quoted to avoid syntax issues, for example:
:'a+b'
You can also use double quotes with this syntax and those quotes behave just like double quotes for strings so they support string interpolation. So this:
:"#{b}bar"
is equivalent to:
"#{b}bar".to_sym
or
(b.to_s + 'bar').to_sym
If you #inspect
your value you'll get a better idea of what it contains:
puts c.inspect
# :foobar