I have the following hash:
a = {
foo: 'bar',
answer: '42'
}
How can I elegantly rename the key :foo
to a new key :test
? If the hash entry for :foo
does not exist, the hash should not be altered.
a[:test] = a.delete(:foo) if a.key?(:foo)