From: "gfx (Goro Fuji)" Date: 2013-09-26T16:05:17+09:00 Subject: [ruby-core:57385] [ruby-trunk - Bug #8953][Open] `str =~ /pattern/` does not call =~ method if (1) str is a String, (2) /pattern/ is a Regexp literal Issue #8953 has been reported by gfx (Goro Fuji). ---------------------------------------- Bug #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 Author: gfx (Goro Fuji) Status: Open Priority: Normal Assignee: Category: Target version: ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0] Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN 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 -- http://bugs.ruby-lang.org/