Iggy Iggy - 4 months ago 11
Ruby Question

How to sum the integer inside array of arrays?

a = [[5, 6], [6, 5], [7, 4], [1, 0, 0], [9, 9], [6, 8], [8, 6], [1, 8, 0], [9, 0]]


How can I sum the integers inside the inner array and return an array of sum of the inner array?

I need it to return
[(5+6), (6+5), (7+4), (1+0+0), ...]
, thus
[11, 11, 11, 1, 18, 14, 14, 9, 9]

Answer
a.map{|a| a.inject(:+)}
# => [11, 11, 11, 1, 18, 14, 14, 9, 9]
Comments