On 10/25/05, Christophe Grandsire <christophe.grandsire / free.fr> wrote:
> Lose the ".class". === automatically looks for the class of the left argument
> ... Basically "a === SomeClass" is the same as "a.class == SomeClass".

On the right track, but it's actually the right argument. That is:

  irb(main):001:0> String === "string"
  => true

That's because the left hand side is the receiver (=== is just another
method). When the receiver is a Class, Class#=== is called.

  irb(main):002:0> String.===( "string" )
  => true

This method takes the class of the argument and compares it to self using ==.

  irb(main):003:0> String == "string".class
  => true

However, if you get the order wrong, you end up calling (in this
example) String#===. String#=== ensures the argument (right hand side)
is_a? String and has the same value. Since the constant String is a
Class, not a String, the comparison fails.

  irb(main):004:0> "string" === String
  => false

A when clause for a case statement evaluates using === with the when
argument as the receiver and case argument as argument. So:

  case "string"
  when String: puts "is string"
  when Integer: puts "is integer"
  else puts "don't know"
  end

is exactly equivalent to:

  if String.===( "string" )
    puts "is string"
  elsif Integer.===( "string" )
    puts "is integer"
  else
    puts "don't know"
  end

Hope that helps clear things up.

Jacob Fugal