On Wed, Feb 25, 2009 at 8:05 AM, 7stud -- <bbxx789_05ss / yahoo.com> wrote:
> Iki 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
> ef =~(obj)
> uts "goodbye"
> nd
> 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 ?<var> 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