Project

General

Profile

Actions

Bug #19418

closed

Checking if a date in an open date range times out when the range starts after the test date

Added by wilhelmsen (Hallgeir Wilhelmsen) about 1 year ago. Updated about 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:112232]

Description

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.

Updated by zverok (Victor Shepelev) about 1 year ago

Range#include 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:

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? 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":

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_VERSION
# => "3.2.0" 
require 'date'
((Date.today + 1)..).include?(Date.today)
# in `include?': cannot determine inclusion in beginless/endless ranges (TypeError)

Updated by jeremyevans0 (Jeremy Evans) about 1 year ago

  • Status changed from Open to Closed

As this is fixed by raising an error in 3.2, I'm closing this.

Actions

Also available in: Atom PDF

Like0
Like0Like0