Sum an array with inject : inject « Array « Ruby






Sum an array with inject


collection = [1, 2, 3, 4, 5]

collection.inject(0) {|sum, i| sum + i}       # => 15

 








Related examples in the same category

1.Building Up a Hash Using Injection
2.Sum of word lengths