Bug #12003
closedUnexpected behavior of === with Range of Date objects
Description
In Ruby 2.2, a Range of Date objects will return true when matched with === for a new Date that falls in the range:
puts ENV['RUBY_VERSION']
# => '2.2.3'
require 'date'
date_range = Date.new(1980, 1, 1)..Date.new(1990, 1, 10)
# => #<Date: 1980-01-01 ((2444240j,0s,0n),+0s,2299161j)>..#<Date: 1990-01-10 ((2447902j,0s,0n),+0s,2299161j)>
puts date_range.cover? Date.new(1989, 10, 13)
# => true
puts date_range === Date.new(1989, 10, 13)
# => true
In Ruby 2.3.0, === returns false for the same date and date range:
puts ENV['RUBY_VERSION']
# => '2.3.0'
require 'date'
date_range = Date.new(1980, 1, 1)..Date.new(1990, 1, 10)
# => #<Date: 1980-01-01 ((2444240j,0s,0n),+0s,2299161j)>..#<Date: 1990-01-10 ((2447902j,0s,0n),+0s,2299161j)>
puts date_range.cover? Date.new(1989, 10, 13)
# => true
puts date_range === Date.new(1989, 10, 13)
# => false
In both Ruby 2.2.3 and Ruby 2.3.0, date comparisons with === return true, i.e, Date.new(1980, 1, 1) === Date.new(1980, 1, 1) # => true.
I believe this may be a regression in the === implementation of Range.
Updated by shugo (Shugo Maeda) about 9 years ago
- Status changed from Open to Closed
Applied in changeset r53635.
- range.c (range_eqq): revert r11113 because rb_call_super() is
called in range_include() and thus r11113 doesn't work when the
receiver Range object consists of non linear objects such as Date
objects.
[ruby-core:72908] [Bug #12003]
Updated by shugo (Shugo Maeda) about 9 years ago
- Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: DONTNEED, 2.3: REQUIRED
Ross Kaffenberger wrote:
In Ruby 2.2, a Range of Date objects will return true when matched with === for a new Date that falls in the range:
(snip)
In Ruby 2.3.0, === returns false for the same date and date range:
Fixed in r53635, but Range#=== is slow when the receiver consists of Date objects, because it uses Enumerable#include? internally.
So I recommend Range#cover? instead.
Updated by naruse (Yui NARUSE) about 9 years ago
- Backport changed from 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: DONTNEED, 2.3: REQUIRED to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: DONTNEED, 2.3: DONE
ruby_2_3 r54403 merged revision(s) 53635.