I have a two-dimensional array as follows:
ary = [["a", 10],["a", 20],["b", 9],["b",7],["c",12]]
I want to sum the numeric values with the same key, building a hash like:
desired_result = {"a"=>30, "b"=>16, "c"=>12}
I can use a hash with a default (0
) and a loop as follows:
rslt = Hash.new(0)
ary.each do |line|
rslt[line[0]] += line[1]
end
But I want to avoid the loop and use enumeration functions. I came up with the following (quite ugly) expression:
rslt = ary.group_by {|a| a[0]}.map {|k,v| [k, v.map {|v| v[1]}.reduce(:+)]}.to_h
which is much harder to read than the loop-version.
Is there a way to do this more elegantly without a loop?