I’m trying to create a Hash from local variables (tossing out vars that don’t exist). Something like this:
foo = "bar"
baz = "qiz"
["foo", "baz", "nothing"].something_awesome
#=>
{"foo" => "bar", "baz" => "qiz"}
I’ve tried a bunch of different combinations from Array and Enumerable, but can’t find the right combo. Any ideas?
Thanks for your help!
Building on top of Jin’s answer is a slightly more idiomatic version that swallows exceptions:
IMHO though
evalshould feel dirty/risky and you might want to reconsider your approach in general.