From: Krzysieq Date: 2008-03-22T00:02:50+09:00 Subject: Re: Newbie question : private / public methods ------=_Part_1056_21943719.1206111780656 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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... Mateusz: You changed the name of the test class, that's why the error. Cheers, Chris 2008/3/21, Stefano Crocco : > > On Friday 21 March 2008, krzysieq Gazeta.pl wrote: > > Hi, > > > > I'm new both to this mailing list and to Ruby. Here's an example, > question > > will come later. > > > > >> private.rb > > > > class PrivateMethodTest > > private > > def privateMethod(the_string) > > 'haha! ' + the_string > > end > > > > public > > def publicMethod(the_string) > > self.privateMethod(the_string) > > end > > end > > > > >> private_test.rb > > > > require 'private.rb' > > require 'test/unit' > > > > class PrivateMethodTestTest < Test::Unit::TestCase > > > > def test_publicMethod > > var = PrivateMethodTest.new() > > assert_equal('haha! test', var.publicMethod('test')) > > end > > end > > > > Now the question: why is it, that when the self. (bolded in the code) is > > there, the test fails, whereas if I remove it, the test is successful? > This > > is the failure message: > > > > 1) Error: > > test_publicMethod(PrivateMethodTestTest): > > NoMethodError: private method `privateMethod' called for > > # > > ./private.rb:9:in `publicMethod' > > private_test.rb:8:in `test_publicMethod' > > > > What am I missing? > > > > Thanks for any help and sorry if this question is silly :) > > Cheers, > > Chris > > > Ruby implements private methods as methods which can't be called using an > explicit receiver (i.e, using the form obj.method_name), but only using > the > implicit receiver (that is, method_name). Since the implicit receiver is > always self, this ensures that private instance methods can only be called > by > instance methods of that class (or of derived classes) and can't be > accessed > by other instances of the same class. An unpleasant side effect of this is > that you can't call private methods using the explicit receiver even when > that > receiver is equal to the implicit one (that is, self), which is the > situation > you discovered. > > ------=_Part_1056_21943719.1206111780656--