How to replace a hash key with another key

Manish Das picture Manish Das · Jun 2, 2011 · Viewed 99.7k times · Source

I have a condition where, I get a hash

  hash = {"_id"=>"4de7140772f8be03da000018", .....}

and I want this hash as

  hash = {"id"=>"4de7140772f8be03da000018", ......}

P.S: I don't know what are the keys in the hash, they are random which comes with an "_" prefix for every key and I want no underscores

Answer

gayavat picture gayavat · Oct 10, 2013
hash[:new_key] = hash.delete :old_key