From: "Darryl L. Pierce" Date: 2011-10-04T23:53:09+09:00 Subject: Re: Operator Overloading On 10/04/2011 10:03 AM, Brian Candler wrote: > Darryl Pierce wrote in post #1024950: >> Define a + method on your Point class: >> >> def +(other) >> self.x += other.x >> self.y += other.y >> self.z += other.z >> self >> end > > Warning!! That will *mutate* the point passed as the left-hand argument > of the '+'! > > I would suggest instead: > > def +(other) > Point.new(x+other.x, y+other.y, z+other.z) > end DOH! Good point! > (Of course, 'x' and 'self.x' are the same; you can write 'self.x' if you > think it's clearer) I don't think so. 'x' is a local variable, while '@x' is an instance variable. And since the fields are instance variables they have to be prefixed with '@'. -- Darryl L. Pierce http://mcpierce.multiply.com/ "What do you care what people think, Mr. Feynman?"