I have a hash like:
h = {\'name\' => \'sayuj\',
\'age\' => 22,
\'project\' => {\'project_name\' => \'abc\',
\'dur
This is an answer to a reasonably old question, but I happened upon it while implementing something similar, thought I'd chime in for a more efficient method.
For the simple, two level deep hash like above, you can also do something like this:
d = h.inject({}) {|copy, (key, value)|
copy[key] = value.dup rescue value; copy
}
I ran a test on a hash of hashes with 4k elements, each a few hundred bytes, and it was about 50% faster than the Marshal.dump/load
Of course, it's not as complete, as it won't work if you have a hash as, e.g., the value of the 'project_name' field, but for a simple 2 level hash, it works great / faster.