Operator Overload

T

Trevor Hinesley

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
 
R

(r.*n){2}

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

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)
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
 
T

Trevor Hinesley

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.
 
J

Jeremy Bopp

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.

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
def -(other)
self + other
end
end
b = 2 - 4
puts b
 
E

Eric Christopherson

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)
=A0return -a
end
b =3D 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.

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

Second, your "b =3D 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 -@
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 =3D 2; puts -n" would work.

Finally, 2 and 4 are Fixnums, not instances of Overloading. Perhaps
you're confused because you have "b =3D 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
# Redefined minus
def -(a)
return self + a
end

b =3D 2-4
puts "#{b}" # prints 6
end


# OR


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

b =3D 2 + -(4)
puts "#{b}" # prints 6
end


# OR



class Overloading
attr_accessor :value

def initialize(value)
@value =3D value
end

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

b =3D Overloading.new(2) - Overloading.new(4)
puts "#{b.value}" # prints 6
end
 
R

Robert Klemme

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?

This might be interesting for you:

http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

Kind regards

robert
 
A

Adriano Ferreira

[Note: parts of this message were removed to make it a legal 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. 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
def -(x)
self + x
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 <[email protected]>
To: ruby-talk ML <[email protected]>
Sent: Thu, September 30, 2010 3:10:35 AM
Subject: Re: Operator Overload

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?

This might be interesting for you:

http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

Kind regards

robert
 
R

Robert Klemme

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 j= ust
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
=A0def -(x)
=A0 =A0self + x
=A0end
end

puts 1 - 1 # =3D> 2

This is certainly not something I would want.
Another tip: In Ruby, every expression returns a value, so there's no nee= d 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top