[ruby-core:96068] [Ruby master Feature#13083] Regexp#{match, match?} with a nil argument are deprecated and will raise a TypeError in Ruby 3.0
From:
naruse@...
Date:
2019-12-03 08:57:17 UTC
List:
ruby-core #96068
Issue #13083 has been updated by naruse (Yui NARUSE).
Eregon (Benoit Daloze) wrote:
> > I believe many of us have got used to the original behavior, that is, methods of a Regexp object work permissively and accept nil, and we know we've migrated many pieces of code from `/re/ =~ nilable` / `/re/ === nilable` to `/re/.match?(nilable)` for the sake of performance and readability just as shugo said above.
> >
> > Can't we reconsider this? Or we'll be doomed to back out all those changes we believed to improve performance.
>
> `nilable&.match?(/re/)` would be an easy way to rewrite those cases, no?
>
> @matz and others: what do you think, should we make `Regexp#{match,match?}` consistent and not accept `nil` or accept them as special-case to match the =~ and === operators?
I also received the same feedback from Rails people.
`nilable&.match?(/re/)` sounds a reasonable option, but why do we provide a such pitfall?
----------------------------------------
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-82917
* 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:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>