How to define the assignment operator in Ruby ?

S

Stephane Wirtel

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
 
I

Ilan Berci

Stephane said:
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
 
S

Stéphane Wirtel

Stephanie,
Stephane and not Stephanie :) Stephanie is the firstname for a girl :)
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
 
D

Doug

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,

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
 
G

Gary Wright

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
 
S

Stephane Wirtel

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 ;-)
Gary Wright

Thanks

Stephane
 
R

Ron Fox

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top