mruby-range-ext: implement Range#overlap? method

Which is implemented in CRuby recently.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2023-09-21 15:28:06 +09:00
parent 393aaada64
commit 384d0e2d21
+8
View File
@@ -96,4 +96,12 @@ class Range
# 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