You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Combined with #14487, it would be useful to have Range methods that, when given another range, would return a new range that contains the union or intersection of both ranges.
For example:
a =1..5
b =3..7
a.union(b) # => 1..7
a.intersection(b) # => 3..5
If there is no overlap between them, I'm not 100% sure what should happen but I feel like returning nil makes sense.
a =1..5
c =10..20
a.union(c) # => nil
a.intersection(c) # => nil
There are probably some other edge cases to work out, as well, such as:
what happens when ranges are infinite in one direction?
should the union be infinite in that direction?
should the intersection use whichever one has a finite value?
for ranges like 1...10 and 10..20 where there's nooverlap, but they are adjacent:
should the union be 1..20 — the union of adjacent ranges providing continuity
should the intersection be nil (or whatever the non-overlap result is deemed to be)?
should (1...10).union(1..10) be 1..10?
should (1...10).intersection(1..10) be 1...10?
any other edge cases?
The text was updated successfully, but these errors were encountered:
Using nil to signal there's no overlap is a bit nasty because you'll need nil handling in order to do something with the result. I'm not sure what's the best solution. Raising may be an option, but having no overlap is not really exceptional. Theoretically we could also use something like 0...0 to express an empty range. This would be formally correct, but may be a cause for confusion because Range#begin is usually expected to be part of the range.
About the edge cases: Considering adjacency makes sense. x < 10 || x >= 10 is continuous (but no intersection).
Combined with #14487, it would be useful to have
Range
methods that, when given another range, would return a new range that contains the union or intersection of both ranges.For example:
If there is no overlap between them, I'm not 100% sure what should happen but I feel like returning
nil
makes sense.There are probably some other edge cases to work out, as well, such as:
union
be infinite in that direction?intersection
use whichever one has a finite value?1...10
and10..20
where there's nooverlap, but they are adjacent:1..20
— the union of adjacent ranges providing continuitynil
(or whatever the non-overlap result is deemed to be)?(1...10).union(1..10)
be1..10
?(1...10).intersection(1..10)
be1...10
?The text was updated successfully, but these errors were encountered: