=== is not a symmetric operator?

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?

2010/8/23 Ralph S. [email protected]:

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

Hi,

In message “Re: === is not a symmetric operator?”
on Mon, 23 Aug 2010 22:52:11 +0900, Ralph S.
[email protected] writes:

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

No.

          matz.

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.
[email protected] 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.”?

2010/8/23 Ralph S. [email protected]:

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

On Mon, Aug 23, 2010 at 8:52 AM, Ralph S. [email protected]
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.” 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”.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs