Hi --

On Wed, 2 Jun 2004, Luke A. Kanies wrote:

> If I run the following code:
> 
> if Hash === Hash
>      puts "yup"
> end
> 
> I get no output.  From what I can tell, this means that I can't test 
> constants in a when statement.  I need something like the following:
> 
> array.each { |member|
>    case member.class
>      when Object::Subclass
>        next
>    end
>    stuff
> }
> 
> and if I can't test the equality of constants with === (which is what 
> 'when' uses; why is it a separate operator?), then I can't use them as 
> values in a when statement.  How do I do this?

=== is separate because it's actually the case operator, not an
equality operator.  Actually it's a method; you can define it for your
own classes, to get them to behave how you wish in case statements. 

Class#=== is defined to test whether something has the given class
among its ancestors:

  case ""
    when Fixnum then puts "number?!"
    when String then puts "string!"
    else puts "mystery"
  end

  # =>  string!

(It's kind of weird-looking when used on its own:

   String === ""    # true

but it's mainly designed with case statements in mind.)

Since Hash is not an instance of Hash, but rather of Class, you're
getting false.  Compare with:

   Class === Hash   #  true

So... what you really want to do is:  

  array.each { |member|
    case member           # not member.class
      when Object::Subclass

etc.  


David

-- 
David A. Black
dblack / wobblini.net