Better Ruby Hash Handling

You’ll be hard pressed to escape working with hashes in Ruby, but don’t make using them a painful experience. Here are some helpful tips that I’ve collected that I would like to share.

slice over a helper method

I see this more often then I care to remember:

def convert(hash)
{
key: hash[:key],
key2: hash[:key2]
}
end

Hash has this built in and performs the same as above.

hash.slice(:key, :key2)

churning values out into an array

[hash[:key], hash[:key2], hash[:key3]]

values_at will perform this with a little less mess.

hash.keys(:key, :key2, :key3)

merge can be a big help

def convert(hash)
{
somekey: hash[:somekey],
another: another
}
end

Instead, maybe this:

hash.slice(:somekey).merge(another: another)

Comments