123456789_123456789_123456789_123456789_123456789_

Class: Range

Constant Summary

::ActiveSupport::RangeWithFormat - Prepended

RANGE_FORMATS

Instance Method Summary

::ActiveSupport::RangeWithFormat - Prepended

#to_formatted_s
#to_fs

Convert range to a formatted string.

::ActiveSupport::CompareWithRange - Prepended

#===

Extends the default Range#=== to support range comparisons.

#include?

Extends the default Range#include? to support range comparisons.

Instance Method Details

#overlap?(other) ⇒ Boolean Also known as: #overlaps?

Raises:

  • (TypeError)
[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/range/overlap.rb', line 8

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

#overlaps?(other)

Alias for #overlap?.

[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/range/overlap.rb', line 39

alias :overlaps? :overlap?

#sole

Returns the sole item in the range. If there are no items, or more than one item, raises ::Enumerable::SoleItemExpectedError.

(1..1).sole   # => 1
(2..1).sole   # => Enumerable::SoleItemExpectedError: no item found
(..1).sole    # => Enumerable::SoleItemExpectedError: infinite range cannot represent a sole item
[ GitHub ]

  
# File 'activesupport/lib/active_support/core_ext/range/sole.rb', line 10

def sole
  if self.begin.nil? || self.end.nil?
    raise ActiveSupport::EnumerableCoreExt::SoleItemExpectedError, "infinite range '#{inspect}' cannot represent a sole item"
  end

  super
end