[ruby-core:112232] [Ruby master Bug#19418] Checking if a date in an open date range times out when the range starts after the test date

Issue #19418 has been reported by wilhelmsen (Hallgeir Wilhelmsen). ---------------------------------------- Bug #19418: Checking if a date in an open date range times out when the range starts after the test date https://bugs.ruby-lang.org/issues/19418 * Author: wilhelmsen (Hallgeir Wilhelmsen) * Status: Open * Priority: Normal * ruby -v: 3.1.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- ` require 'date' ((Date.today + 1)..).include?(Date.today) ` is expected to return `false`. It never return a value, as like it is in a never ending loop. `((Date.today)..).include?(Date.today)` however, returns `true` right away. `(2..).include?(1)` also returns false, as expected. I.e. this seems to be a date issue and not a range issue, and it seem to happen when the start date comes after the date to check for. -- https://bugs.ruby-lang.org/

Issue #19418 has been updated by zverok (Victor Shepelev). [Range#include](https://docs.ruby-lang.org/en/3.1/Range.html#method-i-include-3F) basically iterates throughout the range and compares every value with the target value; it requires only two methods: `value.succ` and `value.==`, and never uses anything else. Therefore, it is infinite iteration when nothing was found. This is a generic implementation that works for all classes, not only for `Date`. Simplest test: ```ruby V = Struct.new(:val) do def succ = V.new(val+1) # this will implement iteration end (V.new(2)..).include?(V.new(1)) # -- hangs forever ``` The `include?` though redefined to behave like [Range#cover?](https://docs.ruby-lang.org/en/3.1/Range.html#method-i-cover-3F) for some range types: namely, numbers and strings. (So, your check of `(2..).include?(1)` doesn't prove "something wrong with Date", it proves "something 'wrong' (specialized for historical reason) with numbers".) `cover?` is the right method to check "if something is inside the range": ```ruby require 'date' ((Date.today + 1)..).cover?(Date.today) #=> false, immediately ``` BTW, just discovered the 3.2 tries to lessen the confusion by prohibiting `include?` on infinite ranges (though it is never mentioned in `NEWS`, hm): ```ruby RUBY_VERSION # => "3.2.0" require 'date' ((Date.today + 1)..).include?(Date.today) # in `include?': cannot determine inclusion in beginless/endless ranges (TypeError) ``` ---------------------------------------- Bug #19418: Checking if a date in an open date range times out when the range starts after the test date https://bugs.ruby-lang.org/issues/19418#change-101655 * Author: wilhelmsen (Hallgeir Wilhelmsen) * Status: Open * Priority: Normal * ruby -v: 3.1.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- ` require 'date' ((Date.today + 1)..).include?(Date.today) ` is expected to return `false`. It never return a value, as like it is in a never ending loop. `((Date.today)..).include?(Date.today)` however, returns `true` right away. `(2..).include?(1)` also returns false, as expected. I.e. this seems to be a date issue and not a range issue, and it seem to happen when the start date comes after the date to check for. -- https://bugs.ruby-lang.org/

Issue #19418 has been updated by jeremyevans0 (Jeremy Evans). Status changed from Open to Closed As this is fixed by raising an error in 3.2, I'm closing this. ---------------------------------------- Bug #19418: Checking if a date in an open date range times out when the range starts after the test date https://bugs.ruby-lang.org/issues/19418#change-101656 * Author: wilhelmsen (Hallgeir Wilhelmsen) * Status: Closed * Priority: Normal * ruby -v: 3.1.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- ` require 'date' ((Date.today + 1)..).include?(Date.today) ` is expected to return `false`. It never return a value, as like it is in a never ending loop. `((Date.today)..).include?(Date.today)` however, returns `true` right away. `(2..).include?(1)` also returns false, as expected. I.e. this seems to be a date issue and not a range issue, and it seem to happen when the start date comes after the date to check for. -- https://bugs.ruby-lang.org/
participants (3)
-
jeremyevans0 (Jeremy Evans)
-
wilhelmsen (Hallgeir Wilhelmsen)
-
zverok (Victor Shepelev)