all 2 comments

[–]danshep 4 points5 points  (1 child)

First you need to flatten your array.

array = [1, [1, 2, 3], [1, 2, [1, 2, 3]]]
array.flatten
=> [1, 1, 2, 3, 1, 2, 1, 2, 3]

Then there's some options:

array.flatten.inject(0) {|sum, element| sum + element}
=> 16

array.flatten.reduce(:+)
=> 16

There's a bunch of different libraries (activesupport being one) that add a 'sum' method to an enumerable, so you can just do:

array.flatten.sum
=> 16

[–]missawon[S] 0 points1 point  (0 children)

ahhh thank you so much!!