How do I rename all the keys in a Ruby hash, based on their original key name, for example, adding a prefix to all of them?
hash = { foo: "bar", test: "blah", hello: "world" }
# do something to `hash`
=> { pre_foo: "bar", pre_test: "blah", pre_hello: "world" }
Would I do this with Hash#map
or something similar?
I got something working with help from "How to change all the keys of a hash by a new set of given keys?" modifying it to access the current key with:
Hash[hash.map {|k, v| ["pre_#{k}".to_sym, v] }]
Is there a better way?