Project

General

Profile

Feature #8953

`str =~ /pattern/` does not call =~ method if (1) str is a String, (2) /pattern/ is a Regexp literal

Added by gfx (Goro FUJI) about 7 years ago. Updated almost 3 years ago.

Status:
Assigned
Priority:
Normal
Target version:
-
[ruby-core:57385]

Description

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

Also available in: Atom PDF