Project

General

Profile

Actions

Feature #8953

closed

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

Added by gfx (Goro FUJI) over 10 years ago. Updated over 2 years ago.

Status:
Closed
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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0