# Overriding Assignment

**URL:** <https://rubytalk.org/t/overriding-assignment/977>\
**Category:** ruby-talk\
**Created:** [2 August 2002 20:55 UTC](https://rubytalk.org/t/overriding-assignment/977 "2002-08-02T20:55:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raganwald](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Raganwald](https://rubytalk.org/u/Raganwald)\
**Post date:** [2 August 2002 20:55 UTC](https://rubytalk.org/t/overriding-assignment/977/1 "2002-08-02T20:55:07Z")

</div>

Is it possible to override the assignment operator?

I tried “def operator=”, and while the compiler seems to accept the  
syntax, my method is not called at run time ☹

Thanks in advance…

---

<div class="post-metadata">

**Author:** ![Ned\_Konz](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Ned\_Konz](https://rubytalk.org/u/Ned_Konz)\
**Post date:** [2 August 2002 21:06 UTC](https://rubytalk.org/t/overriding-assignment/977/2 "2002-08-02T21:06:57Z")

</div>

What would be the receiver?

Variables aren’t objects, so they can’t have methods.  
Variables merely point to objects.

You _can_ define methods with names that end in equals signs, if you  
want.

If you do that at the outer scope (i.e. as a private method in  
Object), it might look like you’ve overridden assignment:

def xyz=(something)  
p something  
end

xyz = 123

But, of course, there’s no variable named xyz.

> **···**
>
> On Friday 02 August 2002 01:55 pm, Reginald Braithwaite-Lee wrote:
> 
> > Is it possible to override the assignment operator?
> > 
> > I tried “def operator=”, and while the compiler seems to accept the  
> > syntax, my method is not called at run time ☹
> 
> –  
> Ned Konz  
> [http://bike-nomad.com](http://bike-nomad.com)  
> GPG key ID: BEEA7EFE

---

<div class="post-metadata">

**Author:** ![Ned\_Konz](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Ned\_Konz](https://rubytalk.org/u/Ned_Konz)\
**Post date:** [2 August 2002 21:20 UTC](https://rubytalk.org/t/overriding-assignment/977/3 "2002-08-02T21:20:58Z")

</div>

It works, but you’re just not calling it the right way (though this is  
probably not what you wanted):

class A  
def operator=(something)  
p “here i am #{something}”  
end  
end

a = A.new  
a.operator = 123

> **···**
>
> On Friday 02 August 2002 01:55 pm, Reginald Braithwaite-Lee wrote:
> 
> > I tried “def operator=”, and while the compiler seems to accept the  
> > syntax, my method is not called at run time ☹
> 
> –  
> Ned Konz  
> [http://bike-nomad.com](http://bike-nomad.com)  
> GPG key ID: BEEA7EFE

---

<div class="post-metadata">

**Author:** ![Raganwald](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Raganwald](https://rubytalk.org/u/Raganwald)\
**Post date:** [11 August 2002 21:26 UTC](https://rubytalk.org/t/overriding-assignment/977/4 "2002-08-11T21:26:19Z")

</div>

Ned Konz [ned@bike-nomad.com](mailto:ned@bike-nomad.com) wrote in message news:[200208021420.20760@ned.bike-nomad.com](mailto:200208021420.20760@ned.bike-nomad.com)…

> **···**
>
> > On Friday 02 August 2002 01:55 pm, Reginald Braithwaite-Lee wrote:
> > 
> > > I tried “def operator=”, and while the compiler seems to accept the  
> > > syntax, my method is not called at run time ☹
> > 
> > It works, but you’re just not calling it the right way (though this is  
> > probably not what you wanted):
> > 
> > class A  
> > def operator=(something)  
> > p “here i am #{something}”  
> > end  
> > end
> > 
> > a = A.new  
> > a.operator = 123
> 
> Thanks for your help. As you guessed, that wasn’t what I wanted,  
> because I wanted special behaviour that would always work when  
> assignment was made.
> 
> Oh well.
