From: Brian Candler Date: 2011-10-05T18:00:29+09:00 Subject: Re: Local vs method vs instance Darryl Pierce wrote in post #1025016: > On 10/04/2011 11:42 AM, Chris Hulan wrote: >> but doing it this way makes the x public readable, so should only be >> used if that is needed >> I'd stick with using @x > > AH! That's what I missing, and what he meant by "the method". Okay. The > OP though didn't have the attr_accessor on the fields, though, that I > remember. This was implied by the use of 'other.x'. If you can write 'other.x' then that object must have a method called 'x'. Since we're talking about adding two objects of the same class, this means self also has a method 'x', and you can call this just using 'x'. Now, if you don't want to have accessors on your Point class, then it gets a little bit messy to tinker with the internals of the other object: e.g. @x + other.instance_variable_get(:@x) or @x + other.instance_eval { @x } I think you could also use a 'protected' accessor method, but I've never found the need to use those. -- Posted via http://www.ruby-forum.com/.