Hi --

On Thu, 23 Mar 2006, burke wrote:

> Joe Van Dyk wrote:
>> a = 5
>> puts  Fixnum == a.class   # spits out true
>>
>> # spits out "Who knows what I am.  :-("
>> case a.class
>> when Fixnum
>>   puts "I'm a fixnum!"
>> else
>>   puts "Who knows what I am.  :-("
>> end
>>
>>
>> What's going on here?
>>
>> Joe
>>
>>
>
> I'll admit I only started learning Ruby 3 days ago, so don't hold it
> against me if I misguide you ;),  but I believe switch uses '===' to
> compare, which I think is a more precise match...
>
> 5.class == Fixnum => true
> 5.class === Fixnum => false

(I've fixed your Fux :-)

It's not that it's more precise; it's just different.  For classes,
=== is defined so that:

   X === x

is true if X is the class of x or one of that class's ancestors.  The
idea is to make it easy to do:

   case obj
   when X ...
   when Y ...

But when obj is itself a class, you'll only get a positive if you test
to see whether it's a Class object:

   case 5.class
   when Class     # this will be true


David

-- 
David A. Black (dblack / wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black