Actions
Bug #19977
closed(nil..nil) === x can raise an exception, differing from Range#cover?
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.3.0dev (2023-10-27T03:13:17Z master f9f0cfe785) [arm64-darwin22]
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:
Actions
Like0
Like0Like0Like0Like0Like0Like0