How to define the assignment operator in Ruby?

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 wrote:

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

Best Regards,

Stephane

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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On Feb 21, 4:55 am, Stephane Wirtel <stephane.wir...@descasoft.com> wrote:

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

Stephanie,

Stephane and not Stephanie :slight_smile: Stephanie is the firstname for a girl :slight_smile:

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

Grrr, so I have reason about this operator, I can't override it.

Thanks

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

···

On Feb 21, 2008, at 9:10 AM, Ilan Berci wrote:

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

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

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 :wink:

Gary Wright

Thanks

Stephane

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

Gary Wright wrote:

···

On Feb 21, 2008, at 9:10 AM, Ilan Berci wrote:

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

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