Re: How to define the assignment operator in Ruby ?
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
>
|