Created
November 22, 2013 16:48
-
-
Save jerryclinesmith/7603058 to your computer and use it in GitHub Desktop.
Comparable Range - union, intersect, and check for overlap/less than/greater than with 2 ranges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ComparableRange < Range | |
include Comparable | |
def self.new(r_start, r_end = nil, exclusive = false) | |
r_end ||= r_start | |
r_start, r_end = r_end, r_start if r_end < r_start | |
super | |
end | |
def overlaps(other) | |
return cover? other unless other.is_a? Range | |
cover? other.min or cover? other.max or other.cover? min or other.cover? max | |
end | |
def include?(other) | |
if other.is_a? Range | |
cover? other.min and cover? other.max | |
else | |
super | |
end | |
end | |
def compare(other) | |
if other.is_a? Range | |
return 0 if overlaps(other) | |
return -1 if max < other.min | |
+1 | |
else | |
return 0 if cover? other | |
return -1 if max < other | |
+1 | |
end | |
end | |
alias_method :<=>, :compare | |
def intersection(other) | |
fail ArgumentError.new('Other should be a range') unless other.is_a? Range | |
return nil unless overlaps(other) | |
new_min = [min, other.min].max | |
new_max = [max, other.max].min | |
self.class.new(new_min, new_max) | |
end | |
alias_method :|, :intersection | |
def union(other) | |
fail ArgumentError.new('Other should be a range') unless other.is_a? Range | |
new_min = [min, other.min].min | |
new_max = [max, other.max].max | |
self.class.new(new_min, new_max) | |
end | |
alias_method :&, :union | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment