aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb9
1 files changed, 8 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 92304a23f6..59128007df 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -30,7 +30,14 @@ module Enumerable #:nodoc:
#
# Also calculates sums without the use of a block:
# [5, 15, 10].sum # => 30
- def sum(&block)
+ #
+ # The default identity (sum of an empty list) is zero.
+ # However, you can override this default:
+ #
+ # [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
+ #
+ def sum(identity = 0, &block)
+ return identity unless size > 0
if block_given?
map(&block).sum
else