I have a two hashes that should have same keys like:
a = {a: 1, b: 2, c: 3}
b = {a: 2, b: 3, c: 4}
And I want to sum up each values like this:
if a.keys == b.keys
a.values.zip(b.values).map{|a, b| a+b}
end
But this code doesn't work if the order of keys are different like b = {a: 2, c: 4, b: 3}
.
How can I write the code taking into account about order of keys?
Use
Hash#merge
orHash#merge!
:The block is called with key, old value, new value. And the return value of the block is used as a new value.