# Weird behavior of case/when

**URL:** <https://rubytalk.org/t/weird-behavior-of-case-when/28876>\
**Category:** ruby-talk\
**Created:** [12 July 2006 08:31 UTC](https://rubytalk.org/t/weird-behavior-of-case-when/28876 "2006-07-12T08:31:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Despres](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@Nicolas\_Despres](https://rubytalk.org/u/Nicolas_Despres)\
**Post date:** [12 July 2006 08:31 UTC](https://rubytalk.org/t/weird-behavior-of-case-when/28876/1 "2006-07-12T08:31:29Z")

</div>

I'm getting a very weird result using case/when.

$ cat test.rb  
def test(a)  
&nbsp;&nbsp;case a.class  
&nbsp;&nbsp;when Class  
&nbsp;&nbsp;&nbsp;&nbsp;puts "class"  
&nbsp;&nbsp;when String  
&nbsp;&nbsp;&nbsp;&nbsp;puts "string"  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;puts "else"  
&nbsp;&nbsp;end  
end

test(Float)  
test("hello")  
test(42)  
$ ruby test.rb  
class  
$ ruby --version  
ruby 1.8.4 (2005-12-24) [i686-linux]

I would have exepected the following output:

class  
string  
else

Does anyone have an explanation about this issue?

Cheers,

> **···**
>
> --  
> Nicolas Desprès

---

<div class="post-metadata">

**Author:** ![Kroeger\_Simon\_ext](https://avatars.discourse-cdn.com/v4/letter/k/5daacb/32.png) [@Kroeger\_Simon\_ext](https://rubytalk.org/u/Kroeger_Simon_ext)\
**Post date:** [12 July 2006 08:44 UTC](https://rubytalk.org/t/weird-behavior-of-case-when/28876/2 "2006-07-12T08:44:03Z")

</div>

case uses === which is well defined for obj === class.  
What you do is checking the class of a.class which is a class itself.

To make it short: use 'case a' instead of 'case a.class'

cheers

Simon

> **···**
>
> > -----Original Message-----  
> > From: Nicolas Desprès [mailto:nicolas.despres@gmail.com]  
> > Sent: Wednesday, July 12, 2006 10:31 AM  
> > To: ruby-talk ML  
> > Subject: Weird behavior of case/when
> > 
> > I'm getting a very weird result using case/when.
> > 
> > $ cat test.rb  
> > def test(a)  
> > &nbsp;&nbsp;case a.class  
> > &nbsp;&nbsp;when Class  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "class"  
> > &nbsp;&nbsp;when String  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "string"  
> > &nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "else"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > test(Float)  
> > test("hello")  
> > test(42)  
> > $ ruby test.rb  
> > class  
> > class  
> > class  
> > $ ruby --version  
> > ruby 1.8.4 (2005-12-24) [i686-linux]
> > 
> > I would have exepected the following output:
> > 
> > class  
> > string  
> > else
> > 
> > Does anyone have an explanation about this issue?
> > 
> > Cheers,

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [12 July 2006 08:47 UTC](https://rubytalk.org/t/weird-behavior-of-case-when/28876/3 "2006-07-12T08:47:46Z")

</div>

Nicolas Desprès schrieb:

> I'm getting a very weird result using case/when.  
> (...)

Nicolas, why do you find it weird? Have you read the documentation, for example

&nbsp;&nbsp;&nbsp;[Expressions](http://phrogz.net/ProgrammingRuby/tut_expressions.html#caseexpressions)

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Nicolas\_Despres](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@Nicolas\_Despres](https://rubytalk.org/u/Nicolas_Despres)\
**Post date:** [12 July 2006 08:49 UTC](https://rubytalk.org/t/weird-behavior-of-case-when/28876/4 "2006-07-12T08:49:47Z")

</div>

Thank you. I knew that case uses === but I would have never expected  
that it behaves this way.

Regards,

> **···**
>
> On 7/12/06, Kroeger, Simon (ext) \<simon.kroeger.ext@siemens.com\> wrote:
> 
> > case uses === which is well defined for obj === class.  
> > What you do is checking the class of a.class which is a class itself.
> > 
> > To make it short: use 'case a' instead of 'case a.class'
> 
> --  
> Nicolas Desprès

---

<div class="post-metadata">

**Author:** ![Marcin\_Miel\_ynski](https://avatars.discourse-cdn.com/v4/letter/m/ba9def/32.png) [@Marcin\_Miel\_ynski](https://rubytalk.org/u/Marcin_Miel_ynski)\
**Post date:** [13 July 2006 19:45 UTC](https://rubytalk.org/t/weird-behavior-of-case-when/28876/5 "2006-07-13T19:45:12Z")

</div>

Nicolas Desprès wrote:

> **···**
>
> > On 7/12/06, Kroeger, Simon (ext) \<simon.kroeger.ext@siemens.com\> wrote:
> > 
> > > case uses === which is well defined for obj === class.  
> > > What you do is checking the class of a.class which is a class itself.
> > > 
> > > To make it short: use 'case a' instead of 'case a.class'
> > 
> > Thank you. I knew that case uses === but I would have never expected  
> > that it behaves this way.
> > 
> > Regards,
> 
> It should behave that in a language where classes are objects 😃 (unlike java where instances of java.lang.Class are just proxies to vm internals)
> 
> lopex
