diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 5c556ff98d..4de520f4ed 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -1,3 +1,5 @@ +require File.dirname(__FILE__) + '/enumerable/injections' + module Enumerable #:nodoc: # Collect an enumerable into sets, grouped by the result of a block. Useful, # for example, for grouping records by date. @@ -20,4 +22,14 @@ module Enumerable #:nodoc: groups end end -end + + # Calculates a sum from the elements. Examples: + # + # payments.sum { |p| p.price * p.tax_rate } + # payments.sum(&:price) + # + # This is instead of payments.inject(0) { |sum, p| sum + p.price } + def sum + inject(0) { |sum, element| sum + yield(element) } + end +end
\ No newline at end of file |