I know we can eval a string to get an existing variable like this:
foo = "foo"
bar = "bar"
%w{foo bar}.each do |baz|
puts eval(baz)
end
=> "foo", "bar"
But is it possible to do the opposite, like this?
%w{foo bar}.each do |baz|
eval(baz) = baz
end
I know a hash would work for the purpose, but hash feels like an overkill for just a couple of variables. Is there a better way to do this besides creating instance variables?