Today I Learned
by OneCode

Rename a key in the hash

This post will be short. Have you ever need to rename a key in the hash?

I suppose you had. There are many solutions, but I found one that is handy and takes only one line:

@hash[:new_key] = @hash.delete(:old_key)

delete removes a key and returns its value. We can use it and assign the result of delete to a new key.

Even if this is simple, I recommend you create a helper method that will wrap this code.