Skip to content

Commit

Permalink
mruby-range-ext: implement Range#overlap? method
Browse files Browse the repository at this point in the history
Which is implemented in CRuby recently.
  • Loading branch information
matz committed Sep 21, 2023
1 parent 393aaad commit 384d0e2
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mrbgems/mruby-range-ext/mrblib/range.rb
Expand Up @@ -96,4 +96,12 @@ def min(&block)
# delegate to Enumerable
super()
end

# Compare two ranges and see if they overlap each other
# (1..5).overlap?(4..6) # => true
# (1..5).overlap?(7..9) # => false
def overlap?(other)
raise TypeError, "argument must be a range" unless other.kind_of?(Range)
other.begin == self.begin || self.cover?(other.begin) || other.cover?(self.begin)
end
end

0 comments on commit 384d0e2

Please sign in to comment.