Stefano Crocco
1/7/2009 7:47:00 AM
Alle Wednesday 07 January 2009, Ruby Rabbit ha scritto:
> I stored the class of a variable in a variable and later checked in a
> switch-case. Instead of going into the Fixnum case, it goes into ELSE.
>
> c=23.class # => Fixnum
>
> c == Fixnum # => true
>
> case c; when Fixnum; puts "YES"; else; puts "NO"; end
>
> # => prints NO
>
> # after much head-scratching tried this:
>
> case c; when Fixnum; puts "YES"; when Class; puts "CLASS"; else; puts
> "NO"; end
>
> # prints CLASS
>
> ---
> As a result of this I had to store the class as a string (to_s), so the
> case could work.
> Could someone explain what I am missing here.
> Thx.
The case expression uses the === operator of the objects in the "when"
statements to decide which one needs to be executed. This means that the
expression
case c
when Fixnum then puts "YES"
when Class then puts "CLASS"
else puts "NO"
end
is (more or less) equivalent to the following if expression:
if Fixnum === c then puts "YES"
elsif Class === c then puts "CLASS"
else puts "NO"
end
Now, the documentation for Class#=== (since both Fixnum and Class are object
of class Class) states that it returns true if an instance of the class or of
one of its descendents and false otherwise. In other words, cls === obj
returns true if obj.is_a?(cls) returns true and false otherwise. In your case,
the object you're testing (c) is the class of 23, that is Fixnum. So, when
executing the case expression, ruby does something like this:
Fixnum === c # returns false because c.is_a?(Fixnum), that is
# Fixnum.is_a?(Fixnum) is false
Class === c # returns true because c.is_a?(Class), that is
# Fixnum.is_a?(Class) is true
puts "CLASS"
To get what you want, you have two possibilities:
1) use "case 23" instead of "case c". This way, it will work as you expected,
because 23 is an instance of class Fixnum, so Fixnum === 23 returns true
2) use an if expression instead of a case expression:
if c.is_a?(Fixnum) then #...
elsif c.is_a?(Class) then #...
else #...
end
I hope this helps
Stefano