Skip to content

Instantly share code, notes, and snippets.

@kvzb
Created February 25, 2013 13:55
Show Gist options
  • Save kvzb/5029933 to your computer and use it in GitHub Desktop.
Save kvzb/5029933 to your computer and use it in GitHub Desktop.
Comparing two ranges for intersections. Returns a Range if they intersect, or nil.
class Range
def intersection(other)
return nil if (self.max < other.begin or other.max < self.begin)
[self.begin, other.begin].max..[self.max, other.max].min
end
alias_method :&, :intersection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment