=== is not a symmetric operator?

Discussion in 'Ruby' started by Ralph Shnelvar, Aug 23, 2010.

  1. [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?
    Ralph Shnelvar, Aug 23, 2010
    #1
    1. Advertising

  2. 2010/8/23 Ralph Shnelvar <>:
    > 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/
    Robert Klemme, Aug 23, 2010
    #2
    1. Advertising

  3. [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 <> 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."?
    Ralph Shnelvar, Aug 23, 2010
    #3
  4. 2010/8/23 Ralph Shnelvar <>:
    > 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/
    Robert Klemme, Aug 23, 2010
    #4
  5. Ralph Shnelvar

    Josh Cheek Guest

    On Mon, Aug 23, 2010 at 8:52 AM, Ralph Shnelvar <> 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".
    Josh Cheek, Aug 23, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gazelle
    Replies:
    0
    Views:
    2,783
    Gazelle
    Nov 12, 2003
  2. lookaround
    Replies:
    1
    Views:
    359
    Alexey Smirnov
    Mar 15, 2007
  3. Adam Olsen

    cmp and sorting non-symmetric types

    Adam Olsen, Nov 13, 2007, in forum: Python
    Replies:
    5
    Views:
    271
    Raymond Hettinger
    Nov 14, 2007
  4. Gustavo Rondina

    Storage of symmetric boolean matrix

    Gustavo Rondina, Jun 27, 2009, in forum: C Programming
    Replies:
    9
    Views:
    452
    Flash Gordon
    Jun 29, 2009
  5. Paddy
    Replies:
    9
    Views:
    288
    Paddy
    Aug 17, 2010
Loading...

Share This Page