Harry Ohlsen
7/10/2003 8:23:00 AM
This hits me every time I try to do it and definitely feels non-POLS to me!
Can someone explain why this isn't kosher ...
fred = 123
case fred.class
when Fixnum
puts "Yippee!"
# when <various other types>
else
puts "I don't know what to do with #{fred.class}"
end
I get "I don't know what to do with Fixnum".
I would have thought that fred.class returns a constant instance of Class, namely Fixnum. I would have thought that the explicit "when Fixnum" is comparing it against the same constant instance of Class. Hence, I'd expect it to choose that when clause.
What's really going on?
If I've just lost the plot (it's happened more than once :-) what's the right way to do this kind of thing?
(Obviously, in the real code, the variables I'm do the #class on isn't a constant).
Thanks in advance,
Harry O.