Iñaki Baz C. wrote:
Could I know why case “b” perform the normal regexp comparission instead
of
invoking the re-defined String#=~ method??
ParseTree gem may help you.
$ cat strange.rb
class String
def =~ obj
puts “Sorry, I don’t compare”
end
end
r1 = “string” =~ 1234
r2 = “string” =~ /string/
r3 = “string”.send(:"=~", 1234)
r4 = “string”.send(:"=~", /string/)
$ parse_tree_show strange.rb
s(:block,
s(:class,
:String,
nil,
s(:scope,
s(:defn,
:=~,
s(:scope,
s(:block,
s(:args, :obj),
s(:fcall, :puts, s(:array, s(:str, “Sorry, I don’t
compare”)))))))),
s(:lasgn, :r1, s(:call, s(:str, “string”), :=~, s(:array, s(:lit,
1234)))),
s(:lasgn, :r2, s(:match3, s(:lit, /string/), s(:str, “string”))),
s(:lasgn,
:r3,
s(:call, s(:str, “string”), :send, s(:array, s(:lit, :=~), s(:lit,
1234)))),
s(:lasgn,
:r4,
s(:call,
s(:str, “string”),
:send,
s(:array, s(:lit, :=~), s(:lit, /string/)))))
$
You can see that your case (b) is parsed differently as a :match3,
bypassing the normal method dispatch (:call)