How to replace a hash key with another key

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


Solution 1:

hash[:new_key] = hash.delete :old_key

Solution 2:

rails Hash has standard method for it:

hash.transform_keys{ |key| key.to_s.upcase }

http://api.rubyonrails.org/classes/Hash.html#method-i-transform_keys

UPD: ruby 2.5 method