Ruby’s Enumerable methods inject and reduce combine a collection’s elements through an accumulator. Each block result becomes the accumulator for the next element; the final result is returned. section 10.5.6
Sum an array
With an initial value of 0, each iteration adds the next number to the accumulator, yielding 10. section 10.5.6
>> [1,2,3,4].inject(0) {|acc,n| acc + n }
=> 10
The block receives the running total and the next element. section 10.5.6
If no initial value is given, inject uses the first element as the accumulator and starts yielding elements from the second. Passing a symbol such as :+ uses that method for the reduction. section 10.5.6
>> [1,2,3,4].inject(:+)
=> 10
A symbol can replace the block when the reduction simply calls one method on the accumulator and each element. section 10.5.6
Reduction can aggregate an Enumerable directly, including a File object, avoiding an intermediate array. It can also replace a mutating loop: reducing an array without modifying it allows a summing method to be called repeatedly with the same input. section 12.2.8 section 16.1.1








