qby-kq
Last Updated: February 25, 2016
·
491
· jeroenr
Dsc 0061

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}]
Say Thanks
Respond