# Operator Overload

**URL:** <https://rubytalk.org/t/operator-overload/60230>\
**Category:** ruby-talk\
**Created:** [28 September 2010 01:49 UTC](https://rubytalk.org/t/operator-overload/60230 "2010-09-28T01:49:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trevor\_Hinesley](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@Trevor\_Hinesley](https://rubytalk.org/u/Trevor_Hinesley)\
**Post date:** [28 September 2010 01:49 UTC](https://rubytalk.org/t/operator-overload/60230/1 "2010-09-28T01:49:45Z")

</div>

Hey guys,

I'm slowly getting the hang of Ruby, I'm a Java guy by trade. I really  
like Ruby and am picking up on it pretty quickly, but for some reason my  
overload of the "+" operator is not working. Here is the code:

class Overloading  
def -(a)  
&nbsp;&nbsp;return -a  
end  
b = 2-4  
puts "#{b}"  
end

Basically, I'm trying to turn the minus operator into a plus operator  
for a project to show how operator overloading in Ruby works, but this  
isn't working from some reason, and I'm not sure why.

Help?

Thanks,

Trevor

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

---

<div class="post-metadata">

**Author:** ![\_r\_n\_2](https://avatars.discourse-cdn.com/v4/letter/_/9dc877/32.png) [@\_r\_n\_2](https://rubytalk.org/u/_r_n_2)\
**Post date:** [28 September 2010 03:00 UTC](https://rubytalk.org/t/operator-overload/60230/2 "2010-09-28T03:00:09Z")

</div>

I think you have just defined the class called Overloading and have  
not instantiated it. Did you intend to do this?  
class Overloading  
def -(a)  
&nbsp;&nbsp;return -a  
end  
b = 2-4  
puts "#{b}"  
end

x = Overloading.new  
puts "#{x.-(5)}"  
puts "#{x - 5}" # same as x.-(5) above

Output:  
-2 # this comes from the puts that is part of class definition  
-5  
-5

> **···**
>
> On Sep 27, 9:49 pm, Trevor Hinesley \<trevor.hines...@gmail.com\> wrote:
> 
> > Hey guys,
> > 
> > I'm slowly getting the hang of Ruby, I'm a Java guy by trade. I really  
> > like Ruby and am picking up on it pretty quickly, but for some reason my  
> > overload of the "+" operator is not working. Here is the code:
> > 
> > class Overloading  
> > def -(a)  
> > return -a  
> > end  
> > b = 2-4  
> > puts "#{b}"  
> > end
> > 
> > Basically, I'm trying to turn the minus operator into a plus operator  
> > for a project to show how operator overloading in Ruby works, but this  
> > isn't working from some reason, and I'm not sure why.
> > 
> > Help?
> > 
> > Thanks,
> > 
> > Trevor  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Eric\_Christopherson](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/eric_christopherson/32/1849_2.png) [@Eric\_Christopherson](https://rubytalk.org/u/Eric_Christopherson)\
**Post date:** [28 September 2010 04:12 UTC](https://rubytalk.org/t/operator-overload/60230/3 "2010-09-28T04:12:51Z")

</div>

First, you're not defining the + method -- you're defining -.

Second, your "b = 2-4" again uses the - method, not +.

Third, your implementation of - just returns negative a. I \*think\*  
your understanding is that the expression "x - y" is implicitly  
translated into "x + -y"; this isn't so. "x - y" is translated into  
"x.-(y)", which in this case just returns -y, while ignoring x.

Fourth, if you want to define a \*unary\* minus method, you would do  
something like:  
def -@  
&nbsp;&nbsp;self  
end

I just realized that -@ doesn't override the unary minus in literal  
expressions like "-2"; it only works if you type "-(2)". It seems that  
if you use a variable, however, the parentheses are not required; thus  
"n = 2; puts -n" would work.

Finally, 2 and 4 are Fixnums, not instances of Overloading. Perhaps  
you're confused because you have "b = 2 - 4" inside your Overloading  
class; nevertheless, they are still Fixnums, and the - method you've  
defined only applies to Overloading. Your - method will only be  
invoked if the object on its left side is an Overloading instance. You  
can redefine the operator methods on Fixnum, but it isn't recommended.

So, maybe what you meant was something like one of these:

class Fixnum  
&nbsp;&nbsp;# Redefined minus  
&nbsp;&nbsp;def -(a)  
&nbsp;&nbsp;&nbsp;&nbsp;return self + a  
&nbsp;&nbsp;end

&nbsp;&nbsp;b = 2-4  
&nbsp;&nbsp;puts "#{b}" # prints 6  
end

# OR

class Fixnum  
&nbsp;&nbsp;# Redefined unary minus  
&nbsp;&nbsp;def -@  
&nbsp;&nbsp;&nbsp;&nbsp;return self  
&nbsp;&nbsp;end

&nbsp;&nbsp;b = 2 + -(4)  
&nbsp;&nbsp;puts "#{b}" # prints 6  
end

# OR

class Overloading  
&nbsp;&nbsp;attr\_accessor :value

&nbsp;&nbsp;def initialize(value)  
&nbsp;&nbsp;&nbsp;&nbsp;@value = value  
&nbsp;&nbsp;end

&nbsp;&nbsp;# Redefined minus  
&nbsp;&nbsp;def -(other)  
&nbsp;&nbsp;&nbsp;&nbsp;return Overloading.new(@value + other.value)  
&nbsp;&nbsp;end

&nbsp;&nbsp;b = Overloading.new(2) - Overloading.new(4)  
&nbsp;&nbsp;puts "#{b.value}" # prints 6  
end

> **···**
>
> On Mon, Sep 27, 2010 at 8:49 PM, Trevor Hinesley \<trevor.hinesley@gmail.com\> wrote:
> 
> > Hey guys,
> > 
> > I'm slowly getting the hang of Ruby, I'm a Java guy by trade. I really  
> > like Ruby and am picking up on it pretty quickly, but for some reason my  
> > overload of the "+" operator is not working. Here is the code:
> > 
> > class Overloading  
> > def -(a)  
> > return -a  
> > end  
> > b = 2-4  
> > puts "#{b}"  
> > end
> > 
> > Basically, I'm trying to turn the minus operator into a plus operator  
> > for a project to show how operator overloading in Ruby works, but this  
> > isn't working from some reason, and I'm not sure why.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [30 September 2010 06:10 UTC](https://rubytalk.org/t/operator-overload/60230/4 "2010-09-30T06:10:35Z")

</div>

This might be interesting for you:

[http://blog.rubybestpractices.com/posts/rklemme/019-Complete\_Numeric\_Class.html](http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html)

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 09/28/2010 03:49 AM, Trevor Hinesley wrote:
> 
> > Hey guys,
> > 
> > I'm slowly getting the hang of Ruby, I'm a Java guy by trade. I really  
> > like Ruby and am picking up on it pretty quickly, but for some reason my  
> > overload of the "+" operator is not working. Here is the code:
> > 
> > class Overloading  
> > def -(a)  
> > &nbsp;&nbsp;&nbsp;return -a  
> > end  
> > b = 2-4  
> > puts "#{b}"  
> > end
> > 
> > Basically, I'm trying to turn the minus operator into a plus operator  
> > for a project to show how operator overloading in Ruby works, but this  
> > isn't working from some reason, and I'm not sure why.
> > 
> > Help?

---

<div class="post-metadata">

**Author:** ![Trevor\_Hinesley](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@Trevor\_Hinesley](https://rubytalk.org/u/Trevor_Hinesley)\
**Post date:** [28 September 2010 03:04 UTC](https://rubytalk.org/t/operator-overload/60230/5 "2010-09-28T03:04:45Z")

</div>

Well, the problem is it isn't doing what I want it to.

It just prints -2, when I need it to print 6, since I'm wanting minus to  
become plus basically.

When I do 2-4, I want it to do 2 minus negative four, which will give  
six.

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

---

<div class="post-metadata">

**Author:** ![Adriano\_Ferreira1](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Adriano\_Ferreira1](https://rubytalk.org/u/Adriano_Ferreira1)\
**Post date:** [1 October 2010 13:09 UTC](https://rubytalk.org/t/operator-overload/60230/6 "2010-10-01T13:09:02Z")

</div>

Robert, I get the point. However it's important to notice that there's no such  
thing as operator overloading in Ruby. Things like :+, :-, :\*, etc, are just  
plain methods. You can achieve what you want by opening the Fixnum class and  
redefining the method :+. Important to say is that all instances of this will be  
affected as soon as you make the change.

Eg:

class Fixnum  
&nbsp;&nbsp;def -(x)  
&nbsp;&nbsp;&nbsp;&nbsp;self + x  
&nbsp;&nbsp;end  
end

puts 1 - 1 # =\> 2

Another tip: In Ruby, every expression returns a value, so there's no need to  
explicit the 'return'.

Regards,

Adriano

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> From: Robert Klemme \<shortcutter@googlemail.com\>  
> To: ruby-talk ML \<ruby-talk@ruby-lang.org\>  
> Sent: Thu, September 30, 2010 3:10:35 AM  
> Subject: Re: Operator Overload
> 
> On 09/28/2010 03:49 AM, Trevor Hinesley wrote:
> 
> > Hey guys,
> > 
> > I'm slowly getting the hang of Ruby, I'm a Java guy by trade. I really  
> > like Ruby and am picking up on it pretty quickly, but for some reason my  
> > overload of the "+" operator is not working. Here is the code:
> > 
> > class Overloading  
> > def -(a)  
> > &nbsp;&nbsp;&nbsp;return -a  
> > end  
> > b = 2-4  
> > puts "#{b}"  
> > end
> > 
> > Basically, I'm trying to turn the minus operator into a plus operator  
> > for a project to show how operator overloading in Ruby works, but this  
> > isn't working from some reason, and I'm not sure why.
> > 
> > Help?
> 
> This might be interesting for you:
> 
> [http://blog.rubybestpractices.com/posts/rklemme/019-Complete\_Numeric\_Class.html](http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html)
> 
> Kind regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)\
**Post date:** [28 September 2010 03:42 UTC](https://rubytalk.org/t/operator-overload/60230/7 "2010-09-28T03:42:00Z")

</div>

You're trying to treat minus as the unary version of the operator, but  
in this usage it is the binary operator. It also appears from your  
example that you are trying to overload it for all numbers using a  
non-number class named Overloading. Since you're able to get it to  
return -2 in your test, I assume that you have actually managed to  
overload the operator for at least the Fixnum class.

Try the following:

class Fixnum  
&nbsp;&nbsp;def -(other)  
&nbsp;&nbsp;&nbsp;&nbsp;self + other  
&nbsp;&nbsp;end  
end  
b = 2 - 4  
puts b

> **···**
>
> On 09/27/2010 10:04 PM, Trevor Hinesley wrote:
> 
> > Well, the problem is it isn't doing what I want it to.
> > 
> > It just prints -2, when I need it to print 6, since I'm wanting minus to  
> > become plus basically.
> > 
> > When I do 2-4, I want it to do 2 minus negative four, which will give  
> > six.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [1 October 2010 14:06 UTC](https://rubytalk.org/t/operator-overload/60230/8 "2010-10-01T14:06:40Z")

</div>

Please do not top post.

> Robert, I get the point. However it's important to notice that there's no such  
> thing as operator overloading in Ruby. Things like :+, :-, :\*, etc, are just  
> plain methods.

Adriano, operator "+" and all others are certainly overloaded (as in  
many programming languages btw) because different implementations get  
called depending on the left hand type. Method #coerce is just a way  
to get double dispatch working.

> You can achieve what you want by opening the Fixnum class and

I do not want something in particular (at least for the sake of this  
thread) so I am not sure what you refer to.

> redefining the method :+. Important to say is that all instances of this will be  
> affected as soon as you make the change.

This is precisely the reason to properly implement operators and  
#coerce in new classes that should play nicely with existing classes.

> Eg:
> 
> class Fixnum  
> def -(x)  
> self + x  
> end  
> end
> 
> puts 1 - 1 # =\> 2

This is certainly not something I would want.

> Another tip: In Ruby, every expression returns a value, so there's no need to  
> explicit the 'return'.

Yes, I know. As far as I can see in my blog article there is no  
"return" in the code.

Cheers

robert

> **···**
>
> On Fri, Oct 1, 2010 at 3:09 PM, Adriano Ferreira \<adrfer@yahoo.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
