Why does it return :a and not a? I thought the ":" was reserved for symbols
It's the expected behavior. According to the docs:
Returns the names of the current local variables.
So yes, this just returns an array of symbols.
Furthermore, how would I go about accessing the actual variables through this method?
As noted by Jonathan Camenisch, Ruby 2.1 introduced Binding#local_variable_get
:
a = 2
binding.local_variable_get(:a)
#=> 2
For older Rubies, you could use eval
:
a = 2
eval(:a.to_s)
#=> 2
Is there a reason behind this behavior, what is it?
In Ruby symbols are used for references:
"foo".methods
#=> [:<=>, :==, :===, :eql?, :hash, :casecmp, ...]
Module.constants
#=> [:Object, :Module, :Class, :BasicObject, :Kernel, :NilClass, ...]