Project

General

Profile

Actions

Bug #12003

closed

Unexpected behavior of === with Range of Date objects

Added by rossta (Ross Kaffenberger) over 8 years ago. Updated about 8 years ago.

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

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0