Overloading operators

Is there a shortcut for writing all this?

    def +(otherField)
        @value + otherField.value
    end

    def -(otherField)
        @value - otherField.value
    end

    def *(otherField)
        @value * otherField.value
    end

    def /(otherField)
        @value / otherField.value
    end

I was also wondering if there is a way to implement +=

total = Amount.aud(300)
total += Amount.aud(500)

total → AUD 800

?

cheers
dim

Philip Mak wrote:

···

Is there a shortcut for writing all this?

    def +(otherField)
        @value + otherField.value
    end

    def -(otherField)
        @value - otherField.value
    end

    def *(otherField)
        @value * otherField.value
    end

    def /(otherField)
        @value / otherField.value
    end

Remembering that code within a class definitionbut outside a method
definition gets executed once only, when Ruby reads the class definition
in:

class Test
attr_accessor :value

def initialize(value)
@value = value
end

%w(+ - * /).each {|op|
class_eval %Q{
def #{op}(otherField)
@value #{op} otherField.value
end
}
}
end

a = Test.new(5)
b = Test.new(6)
puts a + b

···

Philip Mak pmak@aaanime.net wrote:

Is there a shortcut for writing all this?

   def +(otherField)
       @value + otherField.value
   end

I was also wondering if there is a way to implement +=

total = Amount.aud(300)
total += Amount.aud(500)

total → AUD 800

If you reimplement “+”, “+=” is reimplemented as well:

class Fixnum
def +(other)
return 5
end
end
=> nil
1 + 1
=> 5
var = 1
=> 1
var += 1
=> 5

Cheers,

···

On Wed, Jan 22, 2003 at 07:29:33AM +0900, Dmitri Colebatch wrote:

Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Dmitri Colebatch wrote:

I was also wondering if there is a way to implement +=

total = Amount.aud(300)
total += Amount.aud(500)

total → AUD 800

No need, since “x += y” is just syntax for “x = x + y”. Once you define
#+, youve got “+=” as well.

But this may not be what you want, if your #+ method returns a new
instance, and you would rather increment the object that total refers to.

In that case, you may want an #increment method and use it like:

total.increment Amount.aud(500)

Or you could define #<< to do the same thing:

total << Amount.aud(500)

The key point is that these two methods operate destructively on the
object referred to by the local variable total, whereas += assigns a new
object to that variable.

Thanks Joel et al for all the responses…

I will use IRB more
I will use IRB more
I will use IRB more

:slight_smile:

cheers
dim

Joel VanderWerf wrote:

···

Dmitri Colebatch wrote:

I was also wondering if there is a way to implement +=

total = Amount.aud(300)
total += Amount.aud(500)

total → AUD 800

No need, since “x += y” is just syntax for “x = x + y”. Once you define
#+, youve got “+=” as well.

But this may not be what you want, if your #+ method returns a new
instance, and you would rather increment the object that total refers to.

In that case, you may want an #increment method and use it like:

total.increment Amount.aud(500)

Or you could define #<< to do the same thing:

total << Amount.aud(500)

The key point is that these two methods operate destructively on the
object referred to by the local variable total, whereas += assigns a new
object to that variable.