From: Robert Klemme Date: 2004-06-10T19:58:44+09:00 Subject: Re: question about private method "wang" schrieb im Newsbeitrag news:dc1b9b9.0406100210.2305b0e@posting.google.com... > wwayya2004@hotmail.com (wang) wrote in message news:... > > In ruby FAQ : > > The visibility keyword private makes a method callable only in a > > function form, and so it can only have self as a receiver. > > But,why i can't write like below? > > > > class Test > > def func > > self.test #use self > > end > > > > private > > def test > > p "test" > > end > > end > > > > t=Test.new > > t.func >>in `func': private method `test' called for > > #NoMethodError) > > Thanks all! > Maybe i wasn't express my thought clearly. > I read "...it can only have self as a receiver..." in FAQ, so i call > "test" with "self" but wrong. What the really mean of "self" in FAQ? The receiver is implicitely 'self' if you invoke a method without explicitely stating the receiver. There's a simple rule: there are no functions in Ruby, only methods. So 'self' points to a valid instance under all circumstances (even in methods belonging to nil, which is different than Java's 'null' and C's (void*)0). >> class Foo; def test; p self; test2; end; private; def test2; p self; end; end => nil >> o=Foo.new => # >> o.test # # => nil Regards robert