Method: Range#sum
- Defined in:
- activesupport/lib/active_support/core_ext/enumerable.rb
#sum(identity = 0) ⇒ Object
Optimize range sum to use arithmetic progression if a block is not given and we have a range of numeric values.
122 123 124 125 126 |
# File 'activesupport/lib/active_support/core_ext/enumerable.rb', line 122 def sum(identity = 0) 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 |