From 29096268ccce2b13e1490c8b673ffe0b498555fc Mon Sep 17 00:00:00 2001
From: Marc-Andre Lafortune <github@marc-andre.ca>
Date: Tue, 14 Apr 2009 01:12:55 -0400
Subject: 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>
---
 activesupport/lib/active_support/core_ext/enumerable.rb | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

(limited to 'activesupport/lib')

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
 
-- 
cgit v1.2.3