From: Robert Klemme Date: 2006-02-16T23:38:28+09:00 Subject: Re: Private methods - only available to oneself? David Vallner wrote: > Also, a minor mental exercise just came to mind. Presuming Ruby > private would work like in C++, consider this code snippet: > > class A > def foo > puts "foo" > end > private :foo > end > > a = A.new > b = A.new > > def b.bar > foo > end > > Should this be valid? Is b an instance of the same class as a? If > not, should the call to #foo still be valid or not? (E.g. should we > consider singleton classes in the method access rules?) It's valid. And it should be IMHO. > On a slightly related note, in case people watching the thread aren't > confused enough as it is: Ruby's private methods are actually > accessible from subclasses. .... and superclasses: irb(main):001:0> class Base irb(main):002:1> def foo() bar() end irb(main):003:1> end => nil irb(main):004:0> class Der < Base irb(main):005:1> def bar() "bar!" end irb(main):006:1> private :bar irb(main):007:1> end => Der irb(main):008:0> Der.new.foo => "bar!" irb(main):009:0> > In any case, I think determining anything based on the notion of "the > same class" in Ruby would only lead to more ambiguity in the language > rather than make it easier to comprehend. The appropriate term when it comes to private methods is "the same instance". :-) > It would also seem very > non-rubyish to me, since as opposed to the C++ language family, a > class is not the only, or sometimes even not even an important > element of defining an object's behaviour. Definitely! Kind regards robert