class Range
:nodoc:
def _empty_range?(b, e, excl)
def _empty_range?(b, e, excl) return false if b.nil? || e.nil? comp = b <=> e comp.nil? || comp > 0 || (comp == 0 && excl) end
def as_json(options = nil) # :nodoc:
def as_json(options = nil) # :nodoc: to_s end
def overlap?(other)
(1..5).overlap?(4..6) # => true
Compare two ranges and see if they overlap each other
Ruby 3.3+
def overlap?(other) raise TypeError unless other.is_a? Range self_begin = self.begin other_end = other.end other_excl = other.exclude_end? return false if _empty_range?(self_begin, other_end, other_excl) other_begin = other.begin self_end = self.end self_excl = self.exclude_end? return false if _empty_range?(other_begin, self_end, self_excl) return true if self_begin == other_begin return false if _empty_range?(self_begin, self_end, self_excl) return false if _empty_range?(other_begin, other_end, other_excl) true end
def sole
(2..1).sole # => Enumerable::SoleItemExpectedError: no item found
(1..1).sole # => 1
than one item, raises Enumerable::SoleItemExpectedError.
Returns the sole item in the range. If there are no items, or more
def sole if self.begin.nil? || self.end.nil? raise ActiveSupport::EnumerableCoreExt::SoleItemExpectedError, "infinite range '#{inspect}' cannot represent a sole item" end super end
def sum(initial_value = 0)
Optimize range sum to use arithmetic progression if a block is not given and
:nodoc:
def sum(initial_value = 0) if block_given? || !(first.is_a?(Integer) && last.is_a?(Integer)) super else actual_last = exclude_end? ? (last - 1) : last if actual_last >= first sum = initial_value || 0 sum + (actual_last - first + 1) * (actual_last + first) / 2 else initial_value || 0 end end end