From: ts Date: 2001-10-07T19:52:14+09:00 Subject: [ruby-talk:22188] Re: Match expressions >>>>> "A" == Alan Moore writes: A> So, given the expression "node1 =~ node2", if node1 is a literal A> regex (i.e., /xyz/ or %{xyz}), Ruby translates the expression to A> "node1.=~(node2)". If node2 is a literal regex, and node1 evaluates A> to a string, Ruby does a "node2.=~(node1)". In both cases, it calls A> the rb_reg_match function directly, as a speed optimization. But if A> node1 doesn't evaluate to a string, Ruby turns it around again and A> does a "node1.=~(node2)", under the assumption that node1 is a user- A> defined object that defines its own "=~" method. Well, both String and Regexp has the method =~. The preferred way in ruby is to use Regexp#=~, i.e to write Regexp =~ Object The case Object =~ Regexp is rewritten in Regexp.=~(Object). If Object is evaluated to a String this is optimized to rb_reg_match() otherwise this is Object.=~(Regexp) to follow the normal call In the case String =~ String, the second string is the pattern to follow the tradition. NODE_MATCH2 and NODE_MATCH3 are just optimizations, when ruby know that it has a Regexp object. Guy Decoux