Hash Transforms in Ruby

I often find myself wanting to perform some kind of transform on a Ruby Hash which results in another Hash. For instance, converting all keys to strings. Ruby’s built-in Hash methods make this a little inconvenient, because all the standard transforms (#map(), #select(), #reject(), etc…) return arrays of pairs instead of hashes:

So I wrote a generalized hash-transform utility method. It takes a Hash and a block and returns a new Hash. The block receives each key/value pair from the original hash, along with a new Hash to populate:

Here are some examples of use:

Obviously any of these methods could have been written directly in terms of regular inject(), but transform_hash() lets you forget about the housekeeping required by inject() and focus on the task at hand. Rails users will probably also note that at least some of these tasks can also be accomplished using ActiveSupport extensions to Hash. transform_hash() is intended for situations where ActiveSupport may not be available, or does not provide exactly the transform you need.

The code is available as a gist, if you find it useful let me know.