From: Dmitri Colebatch Date: 2003-01-22T07:52:16+09:00 Subject: Re: Overloading operators Thanks Joel et al for all the responses.... I will use IRB more I will use IRB more I will use IRB more :) 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. > >