Ruby 2.4+ / Rails - array.sum
i.e. [1, 2, 3].sum # => 6
Ruby pre 2.4 - array.inject(:+)
or array.reduce(:+)
*Note: The #sum
method is a new addition to 2.4 for enumerable
so you will now be able to use array.sum
in pure ruby, not just Rails.