aboutsummaryrefslogtreecommitdiffstats
path: root/lib/range_ext.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/range_ext.rb')
-rw-r--r--lib/range_ext.rb18
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/range_ext.rb b/lib/range_ext.rb
index f1df5e70d..e7e0e903f 100644
--- a/lib/range_ext.rb
+++ b/lib/range_ext.rb
@@ -1,8 +1,24 @@
class Range
def intersection(other)
- return nil if (self.max < other.min or other.max < self.min)
+ return nil unless intersect?(other)
[self.min, other.min].max..[self.max, other.max].min
end
alias_method :&, :intersection
+
+ def intersect?(other)
+ self.max > other.min and other.max > self.min
+ end
+
+ def remove(other)
+ return self if (self.max < other.min or other.max < self.min)
+
+ [].tap do |remaining|
+ remaining << (self.min..other.min-1) if self.min < other.min
+ remaining << (other.max+1..self.max) if other.max < self.max
+ remaining.compact!
+ end
+ end
+ alias_method :-, :remove
+
end