From: "kyanagi (Kouhei Yanagita) via ruby-core" <ruby-core@...>
Date: 2023-10-29T06:39:53+00:00
Subject: [ruby-core:115194] [Ruby master Bug#19977] (nil..nil) === x can raise an exception, differing from Range#cover?

Issue #19977 has been updated by kyanagi (Kouhei Yanagita).


The fix will likely be https://github.com/kyanagi/ruby/tree/bug_19977.

----------------------------------------
Bug #19977: (nil..nil) === x can raise an exception, differing from Range#cover?
https://bugs.ruby-lang.org/issues/19977#change-105108

* Author: kyanagi (Kouhei Yanagita)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.3.0dev (2023-10-27T03:13:17Z master f9f0cfe785) [arm64-darwin22]
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN
----------------------------------------
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



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/