From: lasitha Date: 2009-02-25T16:10:04+09:00 Subject: Re: Extrange String#=~ behaviour On Wed, Feb 25, 2009 at 8:05 AM, 7stud -- wrote: > Iñaki Baz Castillo wrote: >> Hi, I can't understand it: >> >>   class String >>     def =~ obj >>       puts "Sorry, I don't compare" >>     end >>   end >> >> b) ¿? >> irb> "string" =~ /string/ >> 0 >> Turns out using Regexp.new behaves as expected: 07> "string" =~ Regexp.new('string') Sorry, I don't compare --> nil > class Regexp >  def =~(obj) >    puts "goodbye" >  end > end > > puts /string/ =~ "string"  #0 In this case too: 08> Regexp.new('string') =~ "string" goodbye --> nil Ruby 1.9 rdoc for Regexp#=~ hints at special treatment for regexp literals [1]: "This assignment is implemented in the Ruby parser. So a regexp literal is required for the assignment. The assignment is not occur if the regexp is not a literal." But that is in the context of assigning to ? type named captures within a regexp on the LHS, so i don't know if it's relevant. Hope someone who knows the implementation can shed more light. Cheers, lasitha. [1] http://ruby-doc.org/core-1.9/classes/Regexp.html#M001214