# Help, my 'case/when' code doesn't work

**URL:** <https://www.ruby-forum.com/t/help-my-case-when-code-doesnt-work/78267>\
**Category:** Ruby\
**Created:** [December 4, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/help-my-case-when-code-doesnt-work/78267 "2006-12-04T18:33:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [December 4, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/help-my-case-when-code-doesnt-work/78267/1 "2006-12-04T18:33:55Z")

</div>

I’ve been using ruby for several months now so imagine my surprise when  
I wrote something using the ‘case’ construct and I discovered that I  
didn’t understand how to use it! What’s worse is that even after looking  
through the Pickaxe and some code “in the wild” that uses ‘case’ I still  
don’t see what I’m doing wrong.

Here’s the code in question:

def case\_test(obj)  
print "testing via case… "  
case obj.class  
when Array  
puts “obj is a #{obj.class}”  
when String  
puts “obj is a #{obj.class}”  
else  
puts “obj is unknown: #{obj.class}”  
end  
end

def if\_test(obj)  
print "testing via if… "  
klass = obj.class  
if klass == Array  
puts “obj is a #{klass}”  
elsif klass == String  
puts “obj is a #{klass}”  
else  
puts “obj is unknown: #{obj.class}”  
end  
end

case\_test(Array.new)  
if\_test(Array.new)  
case\_test(String.new)  
if\_test(String.new)  
case\_test(Hash.new)  
if\_test(Hash.new)  
cremes$ ruby a.rb  
testing via case… obj is unknown: Array  
testing via if… obj is a Array  
testing via case… obj is unknown: String  
testing via if… obj is a String  
testing via case… obj is unknown: Hash  
testing via if… obj is unknown: Hash

What am I doing wrong here?

---

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [December 4, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/help-my-case-when-code-doesnt-work/78267/2 "2006-12-04T18:41:56Z")

</div>

On 04.12.2006 18:33, removed\_email\_address@domain.invalid wrote:

> I’ve been using ruby for several months now so imagine my surprise when I wrote something using the ‘case’ construct and I discovered that I didn’t understand how to use it! What’s worse is that even after looking through the Pickaxe and some code “in the wild” that uses ‘case’ I still don’t see what I’m doing wrong.
> 
> Here’s the code in question:
> 
> def case\_test(obj)  
> print "testing via case… "  
> case obj.class

Make that “case obj” instead.

> print "testing via if… "  
> case\_test(Array.new)  
> testing via case… obj is unknown: Hash  
> testing via if… obj is unknown: Hash
> 
> What am I doing wrong here?

```
robert
```

---

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [December 4, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/help-my-case-when-code-doesnt-work/78267/3 "2006-12-04T18:45:57Z")

</div>

Alle 18:33, lunedì 4 dicembre 2006, removed\_email\_address@domain.invalid ha scritto:

> end  
> Your code will call Array===obj.class. According to ri aClass===anObject  
> returns true if anObject is an instance of aClass or of one of its  
> descentents (which more or less means if anObject.is\_a?(aClass) returns  
> true). Because of this, all your ‘when’ statements will fail, because  
> obj.class is not an instance of Array or String. To do what you want,  
> you  
> should substitute

case obj.class

with

case obj

Stefano

---

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [December 4, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/help-my-case-when-code-doesnt-work/78267/4 "2006-12-04T18:58:00Z")

</div>

On Monday, December 04, 2006, at 11:44AM, “Stefano C.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> 
> with
> 
> case obj

Sheesh! I _knew_ it was something easy. I feel like a dope…

cr

---

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [December 5, 2006, 1:31am UTC](https://www.ruby-forum.com/t/help-my-case-when-code-doesnt-work/78267/5 "2006-12-05T01:31:43Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Sheesh! I _knew_ it was something easy. I feel like a dope…

Don’t feel like a dope. That is fairly subtle.

Consider this code:

x = foo  
y = foo

case x  
when y  
puts “yes”  
else  
puts “no”  
end

When would you ever expect this to print “no”?

In most cases, it will print yes. If foo is a class,  
it will print no.

Hal

---

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [December 4, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/help-my-case-when-code-doesnt-work/78267/6 "2006-12-04T19:22:05Z")

</div>

removed\_email\_address@domain.invalid wrote:

/ …

> Sheesh! I _knew_ it was something easy. I feel like a dope…

Perhaps, but in point of fact, you are not a dope. You asked the right  
question and got the right answer. A person who can’t ask any questions  
for  
fear of appearing to be a dope, really does end up a a dope, a  
self-fulfilling prophecy.

Your self-fulfilling prophecy is that you will learn anything you want  
to,  
because you care more about learning things than about hiding what you  
don’t know.

Diagnosis: not a dope.
