Project

General

Profile

Bug #16165

Updated by adh1003 (Andrew Hodgkinson) about 5 years ago

In an endless Range, range, I'd expect to be able to use `#include?` just as I do with a Range range that has an end value. end. It would amount to really just a check on whether the argument was greater than or equal to the start value of that Range the range (and likewise, if "startless" ranges are eventually supported, it'd just be a check on the Range's end value, end, or if it were possibly to have both a startless _and_ endless Range, range, always return `true`). 

 In Ruby 2.6.4, behaviour is unexpected. I "need to know" if a Range range is endless and implement the check myself, because using `#include?` results in `RangeError (cannot get the last element of endless range)`. The reason this feels like a bug, apart from being merely unhelpful, is because `#cover?` accepts a single value rather than a Range range too - and _this_ behaves exactly as we might expect. I can ask if `(1..).cover?(0)` and get false, or `(1..).cover?(2000)` and get `true`, just like I'd expect. 

 It's weird that `#cover?` works, but `#include?` throws an unusual exception. In my particular use case I have an array of Ranges ranges for various periods discrete groups of time that typically terminates in an endless Range range for "everything afterwards". Being afterwards", and being able to write clear, simple code that treats each of these the same to see if a given discrete time falls into a particular time "bucket" is obviously valuable. "bucket". It'd be ugly to have to check each time to see if a given Range the "bucket" was endless and treat it specially, just because `#include?` would otherwise raise an exception. In this case, at least, `#cover?` comes to the rescue :-) 

 If there's a general agreement that this should be a thing, I could look into producing a patch to have `#include?` be a bit better behaved when dealing with endless ranges.

Back