3

I have:

require 'date'

pap1 = Date.parse('1968-06-12')
pap2 = Date.parse('1968-12-31')
dat = Date.parse('1968-06-12')
dat2 = dat + 5 # => #<Date: 1968-06-17 ((2440025j,0s,0n),+0s,2299161j)>

In the example below, I want to test if a date falls in a range of dates. I expect the date range pap1..pap2 to cover dat and dat2. The case equality operator should count dat as within the range of pap1 and pap2:

case dat
when (pap1..pap2)
  puts 'in range'
else
  puts 'not in range'
end
# >> not in range

(pap1..pap2).cover?(dat)              # => true
(pap1..pap2).include?(dat)            # => true
(pap1..pap2) === dat                  # => false
puts 'works' if (pap1..pap2) === dat  # => nil

(pap1..pap2).cover?(dat2)              # => true
(pap1..pap2).include?(dat2)            # => true
(pap1..pap2) === dat2                  # => false
puts 'works' if (pap1..pap2) === dat2  # => nil

But it doesn't. Am I missing something?

  • 3
    Cannot be reproduced. – sawa May 09 '17 at 10:46
  • Regardless of that, if you have pinned down that whatever is wrong with your environment is with `===`, and you know that `case` relies on `===`, then you should simply ask about `===`, not about `case`, making the question simpler. – sawa May 09 '17 at 10:54
  • 1
    What version of ruby are you using? This works fine for me in e.g. ruby 2.3.1 – omnikron May 09 '17 at 10:54
  • Hmm. I get the same result on two systems. Are you seeing `(pap1..pap2) === date` return true? BTW, I did mention case equality operator in the question (edited to mention === explicitly) and gave the version as 2.3.0. Thanks for giving this a try. – Daniel Doherty May 09 '17 at 10:57
  • I'll try to install 2.3.1 and see if I get the same. – Daniel Doherty May 09 '17 at 11:03
  • Are you testing `(pap1..pap2) === date` or `puts 'works' if (pap1..pap2) === date`? The latter should always return `nil` regardless of the condition. – sawa May 09 '17 at 11:03
  • @sawa, I understand. Just included to puts to generate output. The first is the real test, which ought to return `true`. – Daniel Doherty May 09 '17 at 11:16
  • OK. Just installed ruby 2.3.1 and the test passes. A bug in 2.3.0? – Daniel Doherty May 09 '17 at 11:22
  • @sawa, could you try this with 2.3.0? If you get the same, I'll try to submit a bug report. Seems like a pretty serious bug if verified. – Daniel Doherty May 09 '17 at 11:32
  • OK. Looks like this is a known bug. I assume the fix will be backported at some point. [Bug Report](https://bugs.ruby-lang.org/issues/12003) – Daniel Doherty May 09 '17 at 11:46
  • I actually do not believe you enough to spend my time testing them. What exactly is your code, and what result do you get? – sawa May 09 '17 at 11:47
  • @sawa What do you mean 'what is you code' and 'what result do you get'? I included both my code and the results, via `# =>` comments in my OP. Don't test it, it's a known bug, so the ruby maintainers believed it, thank goodness. – Daniel Doherty May 09 '17 at 11:51
  • Okay. Well done. – sawa May 09 '17 at 13:57

1 Answers1

4

This is apparently a known bug in Ruby 2.3.0, fixed at some point in 2.3.1. Here is the bug report Bug Report on === with Dates. Answer: upgrade.

  • 1
    It was fixed in [this commit](https://github.com/ruby/ruby/commit/62b750bb55916e526fb53cb4c19da777872c27f6). – Tom Lord May 09 '17 at 12:19