Issue #8953 has been updated by jeremyevans0 (Jeremy Evans).
Status changed from Assigned to Closed
This was fixed in Ruby 2.2:
```
$ ruby22 -ve 's = "foo"; class << s; def =~ (rhs); raise "a"; end; end; s =~ /foo/'
ruby 2.2.10p489 (2018-03-28 revision 63023) [x86_64-openbsd]
-e:1:in `=~': a (RuntimeError)
from -e:1:in `<main>'
```
----------------------------------------
Feature #8953: `str =~ /pattern/` does not call =~ method if (1) str is a String, (2) /pattern/ is a Regexp literal
https://bugs.ruby-lang.org/issues/8953#change-94043
* Author: gfx (Goro FUJI)
* Status: Closed
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
The expression `a =~ b` does not call the =~ method in some cases.
I think it is a bug that results from optimizations.
See the following code for details:
Code that does not work as expected (shows nothing):
$ ruby -e 's = "foo"; class << s; def =~ (rhs); raise "a"; end; end; s =~ /foo/' # does nothing
Code that works as expected (raises errors):
$ ruby -e 's = "foo"; class << s; def =~ (rhs); raise "a"; end; end; s.=~ /foo/' # call =~ as a method
$ ruby -e 's = "foo"; class << s; def =~ (rhs); raise "a"; end; end; s =~ -> { /foo/ }.call' # RHS is not a Regexp literal but a Regexp object
$ ruby -e 's = Objec.new; class << s; def =~ (rhs); raise "a"; end; end; s =~ /foo/' # LHS is not a String
--
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>