[ruby-core:95747] [Ruby master Feature#13083] Regexp#{match, match?} with a nil argument are deprecated and will raise a TypeError in Ruby 3.0
From:
eregontp@...
Date:
2019-11-07 18:32:37 UTC
List:
ruby-core #95747
Issue #13083 has been updated by Eregon (Benoit Daloze).
Dan0042 (Daniel DeLorme) wrote:
> Is there a benefit to the change apart from consistency? A concrete benefit I mean.
I think consistency is good here, and also it seems to me `regexp.match?(s)` with `s` being `nil` could actually catch bugs where `s` is not expected to be `nil`.
It seems rather odd to me to validate some input and even accept `nil` as an "OK" value to match against a Regexp.
`nil` means typically "missing value" in such a case, and often deserves its own behavior (e.g., could be empty string is not provided, could be an error, could be special behavior just for `nil`, could be some non-empty default String which would help readability).
In the Rails PR I noticed a case that doesn't but is fairly close to end up in `nil.to_i`, which would likely be a bug/unintended:
https://github.com/rails/rails/pull/37504/files#diff-c226a4680f86689c3c170d4bc5911e96
----------------------------------------
Feature #13083: Regexp#{match,match?} with a nil argument are deprecated and will raise a TypeError in Ruby 3.0
https://bugs.ruby-lang.org/issues/13083#change-82564
* Author: kachick (Kenichi Kamiya)
* Status: Open
* Priority: Normal
* Assignee:
* Target version: 2.7
----------------------------------------
Just for consistency
* patch: https://github.com/ruby/ruby/pull/1506
* spec: https://github.com/ruby/spec/pull/380
Currently behaves as ( ruby --version: ruby 2.5.0dev (2016-12-28 trunk 57228) [x86_64-darwin16] )
~~~ ruby
'string'.__send__(:=~, nil) #=> nil
'string'.match(nil) #=> TypeError: wrong argument type nil (expected Regexp)
'string'.match?(nil) #=> TypeError: wrong argument type nil (expected Regexp)
:symbol.__send__(:=~, nil) #=> nil
:symbol.match(nil) #=> TypeError: wrong argument type nil (expected Regexp)
:symbol.match?(nil) #=> TypeError: wrong argument type nil (expected Regexp)
/regex/.__send__(:=~, nil) #=> nil
/regex/.match(nil) #=> nil
/regex/.match?(nil) #=> false
~~~
Expected to
~~~ruby
'string'.__send__(:=~, nil) #=> nil
'string'.match(nil) #=> nil
'string'.match?(nil) #=> false
:symbol.__send__(:=~, nil) #=> nil
:symbol.match(nil) #=> nil
:symbol.match?(nil) #=> false
/regex/.__send__(:=~, nil) #=> nil
/regex/.match(nil) #=> nil
/regex/.match?(nil) #=> false
~~~
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>