Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reverse Hash.inspect or Array.inspect? (aka .to_s) in Ruby

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

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

I now want to revert this back into a hash. How is this done?

I'm not looking for an explanation on other serialisation techniques, I know them. I just need a way to revert this back so I can save it the right way.

like image 993
Cristiano Betta Avatar asked Aug 15 '11 19:08

Cristiano Betta


1 Answers

The fastest answer is: eval.

my_hash = eval(my_str_hash)
like image 148
Sony Santos Avatar answered Sep 19 '22 16:09

Sony Santos