# A newbie question about case and ===

**URL:** <https://www.ruby-forum.com/t/a-newbie-question-about-case-and/66763>\
**Category:** Ruby\
**Created:** [August 29, 2006, 2:54am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-case-and/66763 "2006-08-29T02:54:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [August 29, 2006, 2:54am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-case-and/66763/1 "2006-08-29T02:54:00Z")

</div>

Hey Guys,

Let me apologize upfront if this is not the appropriate place to ask  
newbie questions. I can repost, if someone can point me to the correct  
location.

Assuming no is offended, my question is:

I read that the case statement uses the === method for comparison of  
each of its clauses. So, why doesn’t the second comparison in the  
following code snippet evaluate identically?

a\_fix\_num = 1  
puts a\_fix\_num.class  
case a\_fix\_num  
when Integer  
puts ‘Yes, this is an Integer subclass’  
else  
puts ‘No, this is not an Integer subclass’  
end

if a\_fix\_num === Integer  
puts ‘Yes, this is an Integer subclass’  
else  
puts ‘No, this is not an Integer subclass’  
end

# Fixnum

# Yes, this is an Integer subclass.

# No, this is not an Integer subclass.

Sonny.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [August 29, 2006, 3:02am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-case-and/66763/2 "2006-08-29T03:02:39Z")

</div>

Sonny C. wrote:

> Hey Guys,
> 
> Let me apologize upfront if this is not the appropriate place to ask  
> newbie questions. I can repost, if someone can point me to the correct  
> location.
> 
> Assuming no is offended, my question is:
> 
> I read that the case statement uses the === method for comparison of  
> each of its clauses. So, why doesn’t the second comparison in the  
> following code snippet evaluate identically?
> 
> a\_fix\_num = 1  
> puts a\_fix\_num.class  
> case a\_fix\_num  
> when Integer  
> puts ‘Yes, this is an Integer subclass’  
> else  
> puts ‘No, this is not an Integer subclass’  
> end
> 
> if a\_fix\_num === Integer  
> puts ‘Yes, this is an Integer subclass’  
> else  
> puts ‘No, this is not an Integer subclass’  
> end

case actually evaluates the other way.

Integer === a\_fix\_num # true

> # Fixnum
> 
> # Yes, this is an Integer subclass.
> 
> # No, this is not an Integer subclass.

Open up a terminal and type in ri Class#===  
(and play around with ri otherwise too:)

> Sonny.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [August 29, 2006, 4:21am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-case-and/66763/3 "2006-08-29T04:21:25Z")

</div>

“Eero S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> > puts ‘Yes, this is an Integer subclass’  
> > case actually evaluates the other way.
> 
> Integer === a\_fix\_num # true

```
That is hilarious!
It's obvious when you think about it but, otherwise, it's very

```

surprising…

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [August 29, 2006, 5:25am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-case-and/66763/4 "2006-08-29T05:25:34Z")

</div>

On Aug 28, 2006, at 9:02 PM, Eero S. wrote:

> case actually evaluates the other way.
> 
> Integer === a\_fix\_num # true

I think the problem is that the visual representation (===)  
of the operator i symmetric and this tends to imply that the  
operator follows associative behavior ((a op b) the same as  
(b op a)) but that isn’t true for === in general.

If the operator token was asymmetric I don’t think it would  
be so confusing. Actually, I wonder why the match operator (=~)  
couldn’t be used instead. I’ve looked into this a bit, but I  
I suspect that there are very few cases where === and =~  
could not be aliases for each other.

Gary W.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [August 29, 2006, 6:29am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-case-and/66763/5 "2006-08-29T06:29:11Z")

</div>

Eero S. wrote:

> case actually evaluates the other way.
> 
> Integer === a\_fix\_num # true

Thanks Eero!
