From: Tim Pease Date: 2007-07-04T03:18:48+09:00 Subject: Re: private method clarification, please On 7/3/07, Gregory Brown wrote: > On 7/3/07, Tim Pease wrote: > > Is this behavior expected? > > > > > > $ cat t.rb > > class C > > def meth1() private_method end > > def meth2() self.private_method end > > def private_method() puts 'you called?' end > > private :private_method > > end > > > > c = C.new > > c.meth1 > > c.meth2 > > > > $ ruby --version > > ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-cygwin] > > > > $ ruby t.rb > > you called? > > t.rb:3:in `meth2': private method `private_method' called for > > # (NoMethodError) > > from t.rb:10 > > > > > > It seems distinctly strange to me that you cannot call a private > > method using the self semantics shown in meth2. Is this the expected > > behavior? > > Hmm... I can't decide is it's strange or not. I always think of using > self.whatever in methods as using 'external' access, in which case, > it's not surprising. You can of course, send() through. > > So yeah, I can see where that behaviour might seem surprising but it's > not particularly surprising to me. > The only reason it seems strange is that you can use the self semantics in some cases -- i.e. when calling a setter method class C def meth3( val ) self.private_setter = val end def private_setter=( val ) puts "you said '#{val}'" end private :private_setter= end c = C.new c.meth3( 'hello' ) #=> you said 'hello' So, that case works, but others do not. Just seems strange. TwP