# === is not a symmetric operator?

**URL:** <https://www.ruby-forum.com/t/is-not-a-symmetric-operator/193727>\
**Category:** Ruby\
**Created:** [August 23, 2010, 3:52pm UTC](https://www.ruby-forum.com/t/is-not-a-symmetric-operator/193727 "2010-08-23T15:52:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 3:52pm UTC](https://www.ruby-forum.com/t/is-not-a-symmetric-operator/193727/1 "2010-08-23T15:52:40Z")

</div>

Is my understanding correct that === is not a symmetric operator? That,  
in fact,  
X === x  
and  
x === X

mean different things?

* * *

irb(main):001:0\> class X  
irb(main):002:1\> end  
=\> nil  
irb(main):003:0\> x = X.new  
=\> #\<X:0x4d3a660\>  
irb(main):004:0\> x === X  
=\> false # this is surprising  
irb(main):005:0\> X === x  
=\> true # this is the test I want … but was surprised when  
# x === X didn’t work.  
irb(main):006:0\>

* * *

Does x === X ever mean anything useful?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 4:20pm UTC](https://www.ruby-forum.com/t/is-not-a-symmetric-operator/193727/2 "2010-08-23T16:20:46Z")

</div>

2010/8/23 Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Is my understanding correct that === is not a symmetric operator? That, in fact,  
> X === x  
> and  
> x === X
> 
> mean different things?

Exactly.

> ```
> # x === X didn't work.
> 
> ```
> 
> irb(main):006:0\>
> 
> * * *
> 
> Does x === X ever mean anything useful?

That depends on what objects x and X refer to. Remember that X is  
only a constant - and not a class or module automatically.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 4:21pm UTC](https://www.ruby-forum.com/t/is-not-a-symmetric-operator/193727/3 "2010-08-23T16:21:56Z")

</div>

Hi,

In message “Re: === is not a symmetric operator?”  
on Mon, 23 Aug 2010 22:52:11 +0900, Ralph S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Is my understanding correct that === is not a symmetric operator? That, in fact,  
| X === x  
|and  
| x === X  
|  
|mean different things?

No.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 5:08pm UTC](https://www.ruby-forum.com/t/is-not-a-symmetric-operator/193727/4 "2010-08-23T17:08:47Z")

</div>

Matz,

Monday, August 23, 2010, 8:17:30 AM, you wrote:

YM\> Hi,

YM\> In message “Re: === is not a symmetric operator?”  
YM\> on Mon, 23 Aug 2010 22:52:11 +0900, Ralph S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

YM\> |Is my understanding correct that === is not a symmetric operator?  
That, in fact,  
YM\> | X === x  
YM\> |and  
YM\> | x === X  
YM\> |  
YM\> |mean different things?

YM\> No.

No!!! … They do mean the same thing???

Or … “No, they do not mean the same thing.”?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/is-not-a-symmetric-operator/193727/5 "2010-08-23T17:26:57Z")

</div>

2010/8/23 Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> YM\> | X === x  
> YM\> |and  
> YM\> | x === X  
> YM\> |  
> YM\> |mean different things?
> 
> YM\> No.
> 
> No!!! … They do mean the same thing???
> 
> Or … “No, they do not mean the same thing.”?

Well, in a way they mean the same thing, i.e. method :=== is invoked  
on the left instance and the right instance is passed as argument.  
How :=== is implemented completely depends on the class (or even  
instance) at hand. Conventionally there is an implementation for  
Class and Module which tests for “instance of” relation. And for  
regular expressions :=== implements regexp matching. Etc.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 5:30pm UTC](https://www.ruby-forum.com/t/is-not-a-symmetric-operator/193727/6 "2010-08-23T17:30:28Z")

</div>

On Mon, Aug 23, 2010 at 8:52 AM, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> irb(main):006:0\>
> 
> * * *
> 
> Does x === X ever mean anything useful?

Typically, you define X#=== to be whatever is meaningful for you. So the  
answer depends on how you define it. Typically, when considering how you  
want to define it, you should be thinking about how you want it used in  
a  
case statement. For example, classes typically are used in a case  
statement  
to check if an object is an instance of that class:

X = Class.new  
x = X.new  
case x  
when String  
puts “Do stringy thing”  
when Regexp  
puts “Do regexy thing”  
when X  
puts “Do Xy thing”  
else  
puts “Don’t know what to do”  
end

Is the same as

X = Class.new  
x = X.new  
if String === x  
puts “Do stringy thing”  
elsif Regexp === x  
puts “Do regexy thing”  
elsif X === x  
puts “Do Xy thing”  
else  
puts “Don’t know what to do”  
end

In your example, note that the reason X === x in this case works, is  
because  
X inherits its === method from Module where “Case Equalityâ€”Returns true  
if  
anObject is an instance of mod or one of modâ€˜s descendents. Of limited  
use  
for modules, but can be used in case statements to classify objects by  
class.” [class Module - RDoc Documentation](http://ruby-doc.org/core/classes/Module.html#M001666)

If you want to think of it as an operator, think of it like the minus  
sign,  
where 2-1 is not equal to 1-2, but probably the best thing would be to  
not  
think of it as an operator, but rather as a method exclusively intended  
for  
the convenience of case statements. Notice that the docs even preceed  
the  
description with “Case Equality”.
