Ruby has these nice moments when you think i like this method but i need to change how it operates and it turns out that that method is already prepared to receive a block to do what you had in mind.
I had a series of prices coming from a source in the following format:
{"5" => 200, "6" => 600, ...}, where the key would be the ID of the object and the value the price.
I had multiple series like those, and I had to produce a hash with the averages for every object according to their occurences. Something like this:
Turns out that .merge accepts a block in which you can control how the combination is performed:
This is pretty neat. I wanted to see how would I do it on my own and give another go to refinements:
Alternatively, you can extend each instance of the hash, removing the refine Hash block of the module definition and doing a old_hash.extend(HashSuperMerge) on every iteration.