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

**URL:** <https://rubytalk.org/t/help-my-case-when-code-doesnt-work/33445>\
**Category:** ruby-talk\
**Created:** [4 December 2006 17:33 UTC](https://rubytalk.org/t/help-my-case-when-code-doesnt-work/33445 "2006-12-04T17:33:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chuck\_Remes](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Chuck\_Remes](https://rubytalk.org/u/Chuck_Remes)\
**Post date:** [4 December 2006 17:33 UTC](https://rubytalk.org/t/help-my-case-when-code-doesnt-work/33445/1 "2006-12-04T17:33:10Z")

</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)  
&nbsp;&nbsp;print "testing via case... "  
&nbsp;&nbsp;case obj.class  
&nbsp;&nbsp;when Array  
&nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{obj.class}"  
&nbsp;&nbsp;when String  
&nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{obj.class}"  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;puts "obj is unknown: #{obj.class}"  
&nbsp;&nbsp;end  
end

def if\_test(obj)  
&nbsp;&nbsp;print "testing via if... "  
&nbsp;&nbsp;klass = obj.class  
&nbsp;&nbsp;if klass == Array  
&nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{klass}"  
&nbsp;&nbsp;elsif klass == String  
&nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{klass}"  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;puts "obj is unknown: #{obj.class}"  
&nbsp;&nbsp;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:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [4 December 2006 17:40 UTC](https://rubytalk.org/t/help-my-case-when-code-doesnt-work/33445/2 "2006-12-04T17:40:04Z")

</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)  
> &nbsp;&nbsp;print "testing via case... "  
> &nbsp;&nbsp;case obj.class

Make that "case obj" instead.

> &nbsp;&nbsp;when Array  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{obj.class}"  
> &nbsp;&nbsp;when String  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{obj.class}"  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is unknown: #{obj.class}"  
> &nbsp;&nbsp;end  
> end
> 
> def if\_test(obj)  
> &nbsp;&nbsp;print "testing via if... "  
> &nbsp;&nbsp;klass = obj.class  
> &nbsp;&nbsp;if klass == Array  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{klass}"  
> &nbsp;&nbsp;elsif klass == String  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{klass}"  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is unknown: #{obj.class}"  
> &nbsp;&nbsp;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?

&nbsp;&nbsp;robert

> **···**
>
> On 04.12.2006 18:33, cremes.devlist@mac.com wrote:

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [4 December 2006 17:44 UTC](https://rubytalk.org/t/help-my-case-when-code-doesnt-work/33445/3 "2006-12-04T17:44:06Z")

</div>

Alle 18:33, lunedì 4 dicembre 2006, cremes.devlist@mac.com ha scritto:

> def case\_test(obj)  
> &nbsp;&nbsp;print "testing via case... "  
> &nbsp;&nbsp;case obj.class  
> &nbsp;&nbsp;when Array  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{obj.class}"  
> &nbsp;&nbsp;when String  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{obj.class}"  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is unknown: #{obj.class}"  
> &nbsp;&nbsp;end  
> 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

&nbsp;&nbsp;case obj.class

with

&nbsp;&nbsp;case obj

Stefano

---

<div class="post-metadata">

**Author:** ![Chuck\_Remes](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Chuck\_Remes](https://rubytalk.org/u/Chuck_Remes)\
**Post date:** [4 December 2006 17:57 UTC](https://rubytalk.org/t/help-my-case-when-code-doesnt-work/33445/4 "2006-12-04T17:57:37Z")

</div>

Sheesh! I \*knew\* it was something easy. I feel like a dope...

cr

> **···**
>
> On Monday, December 04, 2006, at 11:44AM, "Stefano Crocco" \<stefano.crocco@alice.it\> wrote:
> 
> > Alle 18:33, lunedì 4 dicembre 2006, cremes.devlist@mac.com ha scritto:
> > 
> > > def case\_test(obj)  
> > > &nbsp;&nbsp;print "testing via case... "  
> > > &nbsp;&nbsp;case obj.class  
> > > &nbsp;&nbsp;when Array  
> > > &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{obj.class}"  
> > > &nbsp;&nbsp;when String  
> > > &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is a #{obj.class}"  
> > > &nbsp;&nbsp;else  
> > > &nbsp;&nbsp;&nbsp;&nbsp;puts "obj is unknown: #{obj.class}"  
> > > &nbsp;&nbsp;end  
> > > 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

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [4 December 2006 18:20 UTC](https://rubytalk.org/t/help-my-case-when-code-doesnt-work/33445/5 "2006-12-04T18:20:05Z")

</div>

/ ...

> 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.

> **···**
>
> cremes.devlist@mac.com wrote:
> 
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [5 December 2006 00:28 UTC](https://rubytalk.org/t/help-my-case-when-code-doesnt-work/33445/6 "2006-12-05T00:28:51Z")

</div>

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

Consider this code:

&nbsp;&nbsp;&nbsp;x = foo  
&nbsp;&nbsp;&nbsp;y = foo

&nbsp;&nbsp;&nbsp;case x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when y  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "yes"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "no"  
&nbsp;&nbsp;&nbsp;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

> **···**
>
> cremes.devlist@mac.com wrote:
> 
> > Sheesh! I \*knew\* it was something easy. I feel like a dope...
