=== is not a symmetric operator?


R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

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?
 
Ad

Advertisements

R

Robert Klemme

2010/8/23 Ralph Shnelvar said:
Is my understanding correct that =3D=3D=3D is not a symmetric operator? = =A0That, in fact,
=A0X =3D=3D=3D x
and
=A0x =3D=3D=3D X

mean different things?
Exactly.

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

Does x =3D=3D=3D 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

Matz,

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

YM> Hi,

YM> In message "Re: === is not a symmetric operator?"

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."?
 
R

Robert Klemme

2010/8/23 Ralph Shnelvar said:
Matz,

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

YM> Hi,

YM> In message "Re: =3D=3D=3D is not a symmetric operator?"
YM> =A0 =A0 on Mon, 23 Aug 2010 22:52:11 +0900, Ralph Shnelvar <[email protected]=
s32.com said:
YM> |Is my understanding correct that =3D=3D=3D is not a symmetric operat= or? =A0That, in fact,
YM> | =A0X =3D=3D=3D x
YM> |and
YM> | =A0x =3D=3D=3D 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 :=3D=3D=3D is invoked
on the left instance and the right instance is passed as argument.
How :=3D=3D=3D 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 :=3D=3D=3D implements regexp matching. Etc.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Ad

Advertisements

J

Josh Cheek

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

Does x =3D=3D=3D X ever mean anything useful?


Typically, you define X#=3D=3D=3D 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 =3D Class.new
x =3D 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 =3D Class.new
x =3D X.new
if String =3D=3D=3D x
puts "Do stringy thing"
elsif Regexp =3D=3D=3D x
puts "Do regexy thing"
elsif X =3D=3D=3D x
puts "Do Xy thing"
else
puts "Don't know what to do"
end


In your example, note that the reason X =3D=3D=3D x in this case works, is =
because
X inherits its =3D=3D=3D method from Module where "Case Equality=97Returns =
true if
anObject is an instance of mod or one of mod=91s descendents. Of limited us=
e
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top