» Jeremy Kahne

I am a freelance developer. Currently messing with Jekyll


Summing an Array of Hashes in Ruby

31 May 2016 » ruby, til

The merge method on a hash object can accept a block for a key and the two values of the hashes so you can create a new value from the two hashes.    

a = {greeting: 'hello', num: 1}
b = {greeting: ' world', num: 2}

a.merge(b){|key, a_val, b_val| a_val + b_val}
=> {:greeting=>"hello world", :num=>3}

This can be used when summing an array of hashes, for instance.

1
2
3
4
5
6
7
8
[
  {:one=>4, :two=>3, :three=>9},
  {:one=>6, :two=>1, :three=>9},
  {:one=>5, :two=>2, :three=>8},
  {:one=>3, :four=>7},
].each_with_object({}){|z, memo| memo.merge!(z){|key,val1, val2| val1+val2} }

=> {:one=>18, :two=>6, :three=>26, :four=>7}

Related Posts