diff options
author | José Valim <jose.valim@gmail.com> | 2009-08-09 19:03:09 +0200 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-08-09 18:05:58 +0100 |
commit | e0adfa82c05f9c975005f102b4bcaebfcd17d241 (patch) | |
tree | 972d84e64e32570b2aba4e4d24e81f90b4b0045a /activesupport | |
parent | 1185500ff0465aff8686315f1b785884f133adcf (diff) | |
download | rails-e0adfa82c05f9c975005f102b4bcaebfcd17d241.tar.gz rails-e0adfa82c05f9c975005f102b4bcaebfcd17d241.tar.bz2 rails-e0adfa82c05f9c975005f102b4bcaebfcd17d241.zip |
Optimize Range#sum only for integers [#2489]
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 5 | ||||
-rw-r--r-- | activesupport/test/core_ext/enumerable_test.rb | 1 |
2 files changed, 4 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index d68eef8c23..42686cfbd5 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -113,9 +113,10 @@ module Enumerable end class Range #:nodoc: - # Optimize range sum to use arithmetic progression if a block is not given. + # Optimize range sum to use arithmetic progression if a block is not given and + # we have a range of numeric values. def sum(identity=0, &block) - return super if block_given? + return super if block_given? || !(first.instance_of?(Integer) && last.instance_of?(Integer)) actual_last = exclude_end? ? (last - 1) : last (actual_last - first + 1) * (actual_last + first) / 2 end diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 6512754e53..6a31ad32f5 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -64,6 +64,7 @@ class EnumerableTests < Test::Unit::TestCase assert_equal 20, (1..4).sum { |i| i * 2 } assert_equal 10, (1..4).sum assert_equal 6, (1...4).sum + assert_equal 'abc', ('a'..'c').sum end def test_each_with_object |