# === operator

**URL:** <https://www.ruby-forum.com/t/operator/106155>\
**Category:** Ruby\
**Created:** [June 6, 2007, 10:17pm UTC](https://www.ruby-forum.com/t/operator/106155 "2007-06-06T22:17:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [June 6, 2007, 10:17pm UTC](https://www.ruby-forum.com/t/operator/106155/1 "2007-06-06T22:17:17Z")

</div>

Hi,

> > {} === Hash  
> > =\> false
> 
> > case {}  
> > when Hash then true  
> > end  
> > =\> true
> 
> > Hash === {}  
> > =\> true

Why is the first statement false when the second is true? I understand  
case uses === to compare the objects? Perhaps it switches them round as  
in the last statement?

Cheers,

Jon

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [June 6, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/operator/106155/2 "2007-06-06T22:26:45Z")

</div>

Hi –

On Thu, 7 Jun 2007, Jon L. wrote:

> Why is the first statement false when the second is true? I understand  
> case uses === to compare the objects? Perhaps it switches them round as  
> in the last statement?

Yes; === is called on the when expression(s), with the case object as  
argument.

David

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [June 6, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/operator/106155/3 "2007-06-06T22:31:46Z")

</div>

On 6/6/07, Jon L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,

Maybe a more explicit notation helps

> > > {} === Hash  
> > > {}.send(:===, Hash)  
> > > =\> false  
> > > case {}  
> > > when Hash then true  
> > > when Hash.send(:===, {}) then true  
> > > end  
> > > =\> true  
> > > Hash === {}  
> > > =\> true
> 
> Why is the first statement false when the second is true? I understand  
> case uses === to compare the objects? Perhaps it switches them round as  
> in the last statement?  
> No the LHS is the receiver and the RHS is the first argument of the  
> message.  
> {}.send(:===, Hash) the === instance\_method of Hash is called which is  
> inherited from Object and basically means equality, hence false.

Hash.send(:===, {}) the class method of Hash is called which is  
inherited from Class and means is\_a? hence true.

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [June 7, 2007, 5:01am UTC](https://www.ruby-forum.com/t/operator/106155/4 "2007-06-07T05:01:02Z")

</div>

Am Donnerstag, 07. Jun 2007, 05:32:38 +0900 schrieb Robert D.:

> On 6/6/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Yes; === is called on the when expression(s), with the case object as  
> > argument.  
> > Well you are right of course, I said now because I did not understand  
> > what OP meant ☹

Some may say it’s an unexpected behaviour. Others will find  
themselves detecting it as a welcome surprise. This is Ruby  
at its best.

Bertram

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [June 6, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/operator/106155/5 "2007-06-06T22:33:46Z")

</div>

On 6/6/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi –  
>   
> Yes; === is called on the when expression(s), with the case object as  
> argument.  
> Well you are right of course, I said now because I did not understand  
> what OP meant ☹

Robert

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [June 7, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/operator/106155/6 "2007-06-07T13:03:04Z")

</div>

Hi –

On Thu, 7 Jun 2007, Bertram S. wrote:

> at its best.  
> I always thought it was just the logical way to do the case statement.  
> Since you’re testing something about the case object, you may not know  
> what it is or what its === method does:

case x # what is x?  
when 1 …  
when “yes” …  
when C  
when nil …  
end

So I don’t think it would make sense to have a case construct where  
=== was called on x.

David

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [June 7, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/operator/106155/7 "2007-06-07T18:01:54Z")

</div>

On 07.06.2007 13:02, removed\_email\_address@domain.invalid wrote:

> > > what OP meant ☹  
> > > when 1 …  
> > > when “yes” …  
> > > when C  
> > > when nil …  
> > > end
> 
> So I don’t think it would make sense to have a case construct where  
> === was called on x.

Another reason why that would be an odd way to do it: _all_ tests then  
would have to be implemented in x’s class - now how much sense would  
that make to do that? Just think of the type test (i.e. using class  
objects in when clause)…

Kind regards

robert
