aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/enumerable.rb
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2009-04-14 01:12:55 -0400
committerJeremy Kemper <jeremy@bitsweat.net>2009-08-08 17:51:09 -0700
commit29096268ccce2b13e1490c8b673ffe0b498555fc (patch)
tree3fdb677bca442e2967f180db42003032c7aa1912 /activesupport/lib/active_support/core_ext/enumerable.rb
parent3b3798506b403911665c3c24fd055b75d6f6a44f (diff)
downloadrails-29096268ccce2b13e1490c8b673ffe0b498555fc.tar.gz
rails-29096268ccce2b13e1490c8b673ffe0b498555fc.tar.bz2
rails-29096268ccce2b13e1490c8b673ffe0b498555fc.zip
Enumerable#sum now works will all enumerables, even if they don't respond to :size
[#2489 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activesupport/lib/active_support/core_ext/enumerable.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb6
1 files changed, 2 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 434a32b29b..e89b7e392e 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -55,12 +55,10 @@ module Enumerable
# [].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
+ map(&block).sum(identity)
else
- inject { |sum, element| sum + element }
+ inject { |sum, element| sum + element } || identity
end
end