Project

General

Profile

Actions

Bug #19977

closed

(nil..nil) === x can raise an exception, differing from Range#cover?

Added by kyanagi (Kouhei Yanagita) about 1 year ago. Updated about 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.3.0dev (2023-10-27T03:13:17Z master f9f0cfe785) [arm64-darwin22]
[ruby-core:115188]

Description

I investigated Range#=== and Range#cover?, and found that the only difference in behavior between them would be that
(nil..nil) === x could throw an exception.

% ~/tmp/ruby-master/bin/ruby -v
ruby 3.3.0dev (2023-10-27T03:13:17Z master f9f0cfe785) [arm64-darwin22]
% ~/tmp/ruby-master/bin/ruby -e 'p (nil..nil) === "a"'
-e:1:in `===': cannot determine inclusion in beginless/endless ranges (TypeError)

p (nil..nil) === "a"
                 ^^^
        from -e:1:in `<main>'
% ~/tmp/ruby-master/bin/ruby -e 'p (nil..nil).cover?("a")'
true

Is this difference intended?

According to NEWS, Range#=== uses cover? since Ruby 2.6 (For String, since Ruby 2.7).

Following this, (nil..nil) === x should not throw an exception in the same way as (nil..nil).cover?(x).

history:

(nil..nil) === "a" throws an exception since https://github.com/ruby/ruby/commit/04a92a6.
For "linear objects" (Integer, Float, Numeric, Time), it has beed fixed not to throw an exception on https://github.com/ruby/ruby/commit/fb17c83.

related issues:

  • [Bug #15449] Range#=== is not using cover in Ruby 2.6
  • [Bug #18580] Range#include? inconsistency for beginless String ranges
  • [Bug #19533] Behavior of ===/include? on a beginless/endless range (nil..nil) changed in ruby 3.2
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0