From: Krzysieq Date: 2008-03-22T00:28:59+09:00 Subject: Re: Newbie question : private / public methods ------=_Part_1142_23331805.1206113349611 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Well, my story is slightly different. I encountered this in a situation, when I had a private method to do some operations, and a number of public methods to use that one. And these public methods can't call the private using self. It's just something I'm used to doing, and perhaps now I will need to get unused to it :) Maybe it's not dangerous at all, it's just different from what I've seen so far. And people tend to fear what they do not understand... Thanks for explanations. Cheers, Chris 2008/3/21, Stefano Crocco : > > On Friday 21 March 2008, Krzysieq wrote: > > Then it enforces habits, which I've been encouraged to develop anyways - > my > > managers always told me not to use this.doSomething whenever there is no > > risk of ambiguity. Don't You think however, that this is quite harsh - I > > mean, don't You think this might get someone into bigger trouble > sometime? > > I don't know Ruby well enough to think of an example yet, but something > > tells me this "feature" could be tricky... > > > Do you refer to being unable to write self.a_private_method ? I don't > think it > can cause any trouble. It does prevent you from writing code like the > following: > > class C > > def initialize > @x = 2 > end > > def a_method value > self.x = value > end > > private > > def x= value > #set the value of @x to something and do some other thing > end > > end > > What the code is trying to do is define a private method, x= which changes > the > value of the instance variable @x and does some extra processing, then use > this method every time it needs to change the value of @x. However, this > kind > of setter methods (those ending in =) need to be called with an explicit > receiver, since otherwise ruby thinks we want to create the local variable > x. > But, since x= is a private method, it can't be called with an explicit > receiver, either. > > Of course, this is not a big problem: we have to call the method with > another > name (set_x, for example) and everything works again, even if it's not as > pretty as it would have been with x=. > > > Stefano > > ------=_Part_1142_23331805.1206113349611--