From c0678d591ca0669268a01079966e6ddd44573204 Mon Sep 17 00:00:00 2001 From: dreamcat4 Date: Sun, 7 Jun 2009 20:09:12 +0100 Subject: [PATCH 1/2] Ruby 1.9 compatability fix - sum When summing an array of strings and [] empty array: rails/activesupport/lib/active_support/core_ext/enumerable.rb:67: in `sum': wrong number of arguments (0 for 1) (ArgumentError) Source: potionstore, ruby 1.9.1p129, rails 2-3-stable --- .../lib/active_support/core_ext/enumerable.rb | 6 +++++- activesupport/test/core_ext/enumerable_test.rb | 4 ++++ 2 files changed, 9 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index a7eaccf..ccd61d5 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -60,7 +60,11 @@ module Enumerable if block_given? map(&block).sum else - inject { |sum, element| sum + element } + if RUBY_VERSION >= '1.9' + map.reduce(:+) + else + inject { |sum, element| sum + element } + end end end diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 92db977..77183dd 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -56,6 +56,10 @@ class EnumerableTests < Test::Unit::TestCase assert_equal 0, [].sum assert_equal 0, [].sum { |i| i } assert_equal Payment.new(0), [].sum(Payment.new(0)) + + assert_equal ["a","b"], [["a","b"],[]].sum + ab = ["strings", "empty"].sum { |s| s=="strings" ? ["a","b"] : [] } + assert_equal ["a","b"], ab end def test_each_with_object -- 1.6.2.4 From 8df70e67e7d3b198b9fadd6513cf2c41ade9e394 Mon Sep 17 00:00:00 2001 From: dreamcat4 Date: Wed, 10 Jun 2009 10:35:13 +0100 Subject: [PATCH 2/2] Wrap method definition with RUBY_VERSION check --- .../lib/active_support/core_ext/enumerable.rb | 22 +++++++++++++------ 1 files changed, 15 insertions(+), 7 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index ccd61d5..a4ca691 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -54,20 +54,28 @@ module Enumerable # # [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0) # - def sum(identity = 0, &block) - return identity unless size > 0 + if RUBY_VERSION >= '1.9' + def sum(identity = 0, &block) + return identity unless size > 0 - if block_given? - map(&block).sum - else - if RUBY_VERSION >= '1.9' + if block_given? + map(&block).sum + else map.reduce(:+) + end + end + else + def sum(identity = 0, &block) + return identity unless size > 0 + + if block_given? + map(&block).sum else inject { |sum, element| sum + element } end end end - + # Iterates over a collection, passing the current element *and* the # +memo+ to the block. Handy for building up hashes or # reducing collections down to one object. Examples: -- 1.6.2.4