Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > === is not a symmetric operator?

Reply
Thread Tools

=== is not a symmetric operator?

 
 
Ralph Shnelvar
Guest
Posts: n/a
 
      08-23-2010
[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?
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-23-2010
2010/8/23 Ralph Shnelvar <(E-Mail Removed)>:
> 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/

 
Reply With Quote
 
 
 
 
Ralph Shnelvar
Guest
Posts: n/a
 
      08-23-2010
[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> on Mon, 23 Aug 2010 22:52:11 +0900, Ralph Shnelvar <(E-Mail Removed)> 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."?
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-23-2010
2010/8/23 Ralph Shnelvar <(E-Mail Removed)>:
> 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 <ralphs@do=

s32.com> writes:
>
> 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/

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      08-23-2010
On Mon, Aug 23, 2010 at 8:52 AM, Ralph Shnelvar <(E-Mail Removed)> wrote:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cmp and sorting non-symmetric types Adam Olsen Python 5 11-14-2007 09:21 PM
UrlEncode - UrlDecode problem: not symmetric lookaround ASP .Net 1 03-15-2007 11:29 AM
Funny problem about symmetric encryption Alexandre Oberlin Computer Security 5 11-15-2006 11:48 PM
Gigabit symmetric broadband... Bling-Bling NZ Computing 41 04-26-2005 10:15 PM
Frequency Doubler in VHDL with symmetric duty cycle Gazelle VHDL 0 11-12-2003 07:35 PM



Advertisments