diff options
author | José Valim <jose.valim@gmail.com> | 2009-08-09 13:22:06 +0200 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-08-09 15:56:10 +0100 |
commit | 1bd4d1c67459a91415ee73a8f55d2309c0d62a87 (patch) | |
tree | 4862a97d1187ee76c64df4083792af0bfcd1a363 /activesupport/lib | |
parent | d811864e880580549bc1ab73a1ab0be886598e6e (diff) | |
download | rails-1bd4d1c67459a91415ee73a8f55d2309c0d62a87.tar.gz rails-1bd4d1c67459a91415ee73a8f55d2309c0d62a87.tar.bz2 rails-1bd4d1c67459a91415ee73a8f55d2309c0d62a87.zip |
Optimize Range#sum to use arithmetic progression when a block is not given [#2489].
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 9 |
1 files changed, 9 insertions, 0 deletions
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 |