Last Updated: February 25, 2016
·
621
· Jeroen Rosenberg

Cool and concise Array operations

Merge array of hashes into one hash

a = [{a: 1, b: 2,c: 3},{b: 4, d: 1},{b: 3,c: 1}]
a.reduce(&:merge)

=> {:a=>1, :b=>3, :c=>1, :d=>1}

Remove empty hashes from array

a = [{},{a: 1},{},{}]
a.reject(&:empty?)

=> [{:a=>1}]