I have 2 hashes, for example:
{'a' => 30, 'b' => 14} {'a' => 4, 'b' => 23, 'c' => 7}
where a
, b
and c
are objects. How can I sum those hashes' keys to get a new hash like:
{'a' => 34, 'b' => 37, 'c' => 7}
a_hash = {'a' => 30, 'b' => 14} b_hash = {'a' => 4, 'b' => 23, 'c' => 7} a_hash.merge(b_hash){ |k, a_value, b_value| a_value + b_value } => {"a"=>34, "b"=>37, "c"=>7} b_hash.merge(a_hash){ |k, b_value, a_value| a_value + b_value } => {"a"=>34, "b"=>37, "c"=>7}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With