From 1bd4d1c67459a91415ee73a8f55d2309c0d62a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 9 Aug 2009 13:22:06 +0200 Subject: Optimize Range#sum to use arithmetic progression when a block is not given [#2489]. Signed-off-by: Pratik Naik --- activesupport/lib/active_support/core_ext/enumerable.rb | 9 +++++++++ activesupport/test/core_ext/enumerable_test.rb | 2 ++ 2 files changed, 11 insertions(+) diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index e89b7e392e..d68eef8c23 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -111,3 +111,12 @@ module Enumerable !any?(&block) end unless [].respond_to?(:none?) end + +class Range #:nodoc: + # Optimize range sum to use arithmetic progression if a block is not given. + def sum(identity=0, &block) + return super if block_given? + actual_last = exclude_end? ? (last - 1) : last + (actual_last - first + 1) * (actual_last + first) / 2 + end +end diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 1927a0ad0d..6512754e53 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -61,7 +61,9 @@ class EnumerableTests < Test::Unit::TestCase end def test_enumerable_sums + assert_equal 20, (1..4).sum { |i| i * 2 } assert_equal 10, (1..4).sum + assert_equal 6, (1...4).sum end def test_each_with_object -- cgit v1.2.3