Another alternative:
a = [{:a=>1},{:a=>10},{:b=>8},{:c=>7},{:c=>2}] p a.each_with_object({}) { |h, o| h.each { |k,v| (o[k] ||= []) << v } }
It also works when hashes have several key / value combinations, for example:
b = [{:a=>1, :b=>5, :x=>10},{:a=>10, :y=>2},{:b=>8},{:c=>7},{:c=>2}] p b.each_with_object({}) { |h, o| h.each { |k,v| (o[k] ||= []) << v } }
source share