How to define the assignment operator in Ruby ?

Discussion in 'Ruby' started by Stephane Wirtel, Feb 21, 2008.

  1. Hi all,

    How to define the assignment operator for a class ?

    irb(main):012:0> class Test
    irb(main):013:1> def = other
    irb(main):014:2> puts other.class
    irb(main):015:2> end
    irb(main):016:1> end
    SyntaxError: compile error
    (irb):13: syntax error, unexpected '='
    def = other
    ^
    (irb):16: syntax error, unexpected kEND, expecting $end from (irb):16

    Is there a way to solve my issue ?

    Where is my mistake ?

    I don't believe that's possible to assign something to an instance

    Best Regards,

    Stephane
     
    Stephane Wirtel, Feb 21, 2008
    #1
    1. Advertisements

  2. Stephane Wirtel

    Ilan Berci Guest

    Stephanie,

    In ruby, we don't assign to an instance as you mentioned, we assign to a
    variable that can hold any type. We therefore can't override the
    assignment operator.

    BUT you can override an assignment operation.. such as

    class Foo
    def bar= (val)
    @bar = val
    p "I am assigning #{val} to @bar"
    end

    hth

    ilan
     
    Ilan Berci, Feb 21, 2008
    #2
    1. Advertisements

  3. Stephanie,
    Stephane and not Stephanie :) Stephanie is the firstname for a girl :)
    Grrr, so I have reason about this operator, I can't override it.

    Thanks
     
    Stéphane Wirtel, Feb 21, 2008
    #3
  4. Stephane Wirtel

    Doug Guest

    Stephane,

    What exactly are you trying to do? If you are trying to implement a
    method that copies an object, this is supported in the language via
    the dup() or clone() methods:

    str = "I am a string."
    copy_of_str = str.clone
    str.object_id should not equal copy_of_str.object_id

    For your own objects, the default clone and dup methods perform
    shallow copies of state (references are copied). You would have to
    override clone or dup to do anything more complicated.

    class A
    attr_accessor :str
    def initialize(str)
    @str = str
    end
    end

    a = A.new("test")
    b = a.clone

    a.object_id != b.object_id

    but

    a.str.object_id == b.str.object_id

    Is this what you were getting at?

    -Doug
     
    Doug, Feb 22, 2008
    #4
  5. Stephane Wirtel

    Gary Wright Guest

    Setter methods may look like assignment, but semantically they are
    method calls. I think it just confuses matters to call setter method
    invocation an 'assignment operation'.

    The desire to override assignment or define an assignment operator is
    generally indicative of some misunderstanding regarding Ruby's object
    model.

    Gary Wright
     
    Gary Wright, Feb 22, 2008
    #5
  6. The desire to override assignment or define an assignment operator is
    It's not a misunderstanding, I wanted to be sure that it was not
    possible to do it.

    I had a doubt because I discussed with a friend about python and this
    operator, and he tells me that is possible to override it.

    Today, after googling, my friend tells me that's not possible to
    override this operator ;-)
    Thanks

    Stephane
     
    Stephane Wirtel, Feb 22, 2008
    #6
  7. Stephane Wirtel

    Ron Fox Guest

    Exactly.. since assignment in Ruby is really assigning a >reference<
    to an object to a variable rather than copying an existing object
    to a new one. For example:

    a = "some String"
    b = a
    b << " more text"

    print "#{a}"

    Will print "some String more text" not "some String" because a and b
    refer to the same objects rather than distinct copies of an object
    that contains "some String". This is why the dup/clone methods exist.

    Ron
     
    Ron Fox, Feb 22, 2008
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.