I’ve had the need to find the “intersection” of 2 ranges, i.e. the maximal range that is wholly contained within those 2 ranges.
e.g. as a basic example:
(1..5).intersection(3..7) => (3..5)
I was surprised that there is no such method on
Range since it seems generally useful. There are some subtleties to defining it on
Range itself since it’s unclear how it should work for “inverted” ranges (i.e.
(5..1)) and returning
nil when there’s no overlap doesn’t allow ranges to be “closed under intersection” since there’s no generic notion of an empty range.
I put up this PR with my implementation in case anyone has feedback or thinks it’s worth incorporating for others to leverage.
Curious what y’all think.