Methods
- #
- O
- S
Instance Public methods
_empty_range?(b, e, excl) Link
overlap?(other) Link
Compare two ranges and see if they overlap each other
(1..5).overlap?(4..6) # => true
(1..5).overlap?(7..9) # => false
Also aliased as: overlaps?
# 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
sole() Link
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