Class: Range

Inherits:
Object show all
Defined in:
lib/gorillib/enumerable/sum.rb

Overview

:nodoc:

Direct Known Subclasses

IpRange

Instance Method Summary collapse

Instance Method Details

#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.



34
35
36
37
38
# File 'lib/gorillib/enumerable/sum.rb', line 34

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