How can I cancel Hash.inspect or Array.inspect? (aka.to_s) in Ruby

I accidentally saved a Ruby hash for a string in Ruby 1.9 by calling my_hash.to_s , which is equal to my_hash.inspect . This gave me a line like this:

 '{"foo"=>{"bar"=>"baz", "qux"=>"quux"}' 

Now I want to return this back to the hash. How it's done?

I am not looking for an explanation on other serialization methods, I know them. I just need a way to get this back so that I can keep it in the right direction.

+7
source share
2 answers

The fastest answer: eval .

 my_hash = eval(my_str_hash) 
+19
source

eval it.

Of course, this is unsafe for arbitrary input, but you said you know about serialization issues. It will not work for collections containing recursive links or other objects for which eval (x.inspect)! = X.

+6
source

All Articles