# How to define the assignment operator in Ruby?

**URL:** <https://rubytalk.org/t/how-to-define-the-assignment-operator-in-ruby/44672>\
**Category:** ruby-talk\
**Created:** [21 February 2008 12:54 UTC](https://rubytalk.org/t/how-to-define-the-assignment-operator-in-ruby/44672 "2008-02-21T12:54:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephane\_Wirtel2](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@Stephane\_Wirtel2](https://rubytalk.org/u/Stephane_Wirtel2)\
**Post date:** [21 February 2008 12:54 UTC](https://rubytalk.org/t/how-to-define-the-assignment-operator-in-ruby/44672/1 "2008-02-21T12:54:55Z")

</div>

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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
(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

---

<div class="post-metadata">

**Author:** ![Ilan\_Berci](https://avatars.discourse-cdn.com/v4/letter/i/94ad74/32.png) [@Ilan\_Berci](https://rubytalk.org/u/Ilan_Berci)\
**Post date:** [21 February 2008 14:10 UTC](https://rubytalk.org/t/how-to-define-the-assignment-operator-in-ruby/44672/2 "2008-02-21T14:10:54Z")

</div>

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)  
&nbsp;&nbsp;@bar = val  
&nbsp;&nbsp;p "I am assigning #{val} to @bar"  
end

hth

ilan

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Doug1](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@Doug1](https://rubytalk.org/u/Doug1)\
**Post date:** [22 February 2008 00:05 UTC](https://rubytalk.org/t/how-to-define-the-assignment-operator-in-ruby/44672/3 "2008-02-22T00:05:00Z")

</div>

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  
&nbsp;&nbsp;attr\_accessor :str  
&nbsp;&nbsp;def initialize(str)  
&nbsp;&nbsp;&nbsp;&nbsp;@str = str  
&nbsp;&nbsp;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  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
> > (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

---

<div class="post-metadata">

**Author:** ![Stephane\_Wirtel3](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@Stephane\_Wirtel3](https://rubytalk.org/u/Stephane_Wirtel3)\
**Post date:** [21 February 2008 19:59 UTC](https://rubytalk.org/t/how-to-define-the-assignment-operator-in-ruby/44672/4 "2008-02-21T19:59:55Z")

</div>

> 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)  
> &nbsp;&nbsp;@bar = val  
> &nbsp;&nbsp;p "I am assigning #{val} to @bar"  
> end

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

Thanks

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [22 February 2008 00:53 UTC](https://rubytalk.org/t/how-to-define-the-assignment-operator-in-ruby/44672/5 "2008-02-22T00:53:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Stephane\_Wirtel2](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@Stephane\_Wirtel2](https://rubytalk.org/u/Stephane_Wirtel2)\
**Post date:** [22 February 2008 10:45 UTC](https://rubytalk.org/t/how-to-define-the-assignment-operator-in-ruby/44672/6 "2008-02-22T10:45:05Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Ron\_Fox](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Ron\_Fox](https://rubytalk.org/u/Ron_Fox)\
**Post date:** [22 February 2008 10:49 UTC](https://rubytalk.org/t/how-to-define-the-assignment-operator-in-ruby/44672/7 "2008-02-22T10:49:55Z")

</div>

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
