Ruby local variables in scope

Published July 29, 2009 in Ruby

Some neat Ruby esoterica I ran into recently:

a, b = 1, 2
local_variables #=> ["_", "a", "b"]
local_variables.inject({}){|h,v|h.merge v.to_sym => eval(v)}
  #=> {:b=>2, :a=>1, :_=>["_", "a", "b"]}

Now, I’m not quite sure what that’s good for, but it’s neat to know about.