# Case bug when comparing classes...?

**URL:** <https://www.ruby-forum.com/t/case-bug-when-comparing-classes/80621>\
**Category:** Ruby\
**Created:** [December 29, 2006, 11:51am UTC](https://www.ruby-forum.com/t/case-bug-when-comparing-classes/80621 "2006-12-29T11:51:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [December 29, 2006, 11:51am UTC](https://www.ruby-forum.com/t/case-bug-when-comparing-classes/80621/1 "2006-12-29T11:51:30Z")

</div>

Hi,  
I’m encountering weird behavior I don’t understand when creating a  
case expression that does comparison using classes. Consider the  
following:

class SomeClass  
end

def case\_test elem  
case elem.class  
when Object  
puts “Object”  
when String  
puts “String”  
when SomeClass  
puts “SomeClass”  
else  
puts “Unknown: #{elem.class}”  
end  
end

executing:

case\_test Object.new  
case\_test “bla”  
case\_test SomeClass.new  
case\_test 1

yields:

Object  
Object  
Object  
Object

On the other hand, running:

def if\_test elem  
if Object == elem.class  
puts “Object”  
elsif String == elem.class  
puts “String”  
elsif SomeClass == elem.class  
puts “SomeClass”  
else  
puts “Unknown: #{elem.class}”  
end  
end

if\_test Object.new  
if\_test “bla”  
if\_test SomeClass.new  
if\_test 1

results in:

Object  
String  
SomeClass  
Unknown: Fixnum

I’ve not been able to find anything that fits searching the bug  
database or doing a rudimentary google search, and it would seem to me  
that someone else would have come across this before.

Am I missing something?

Thanks,  
-tim

Oh and btw: ruby 1.8.5 (2006-08-25) [i686-darwin8.8.1]

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [December 29, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/case-bug-when-comparing-classes/80621/2 "2006-12-29T12:01:32Z")

</div>

Tim B. wrote:

> ```
> when Object
> 
> ```
> 
> executing:  
> Object  
> elsif SomeClass == elem.class
> 
> that someone else would have come across this before.
> 
> Am I missing something?

Yes ;-).

Using the case construct implies that you’re using the === operator.  
And the === operator of a instance of Class is exactly is\_a?. Which  
means that, since elem.class is a instance of Class, it is an Object,  
and your first test will always match.

You should try this:

def case\_test elem  
case elem  
when String  
puts “String”  
when SomeClass  
puts “SomeClass”  
when Object  
puts “Object”  
else  
puts “Unknown: #{elem.class}”  
end  
end

You need to put Object at the end, and look only at elem. (and you else  
clause will never get executed)

Cheers,

```
Vince
```

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [December 29, 2006, 12:11pm UTC](https://www.ruby-forum.com/t/case-bug-when-comparing-classes/80621/3 "2006-12-29T12:11:34Z")

</div>

That would explain it 🙂 Thanks!
