David V. wrote:
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
It is good to ask questions to come to a better understanding. With
that in mind…
Minkoo S. wrote:
Private/protected meaning of Ruby is unique, and I believe some kind of
elaboration on that point is needed. What method is accessible from the
outer space is quite important in learning a language, isn’t it?
In Ruby, the question “What class does this belong to?” tends to be
deemphasized in favor of “What does this object do?”. Considering that
point of view tells me that instance based protection is not out of
place at all in the Ruby scheme of things.
Also, Ruby’s instance based protect is not unique. The protection
scheme in Eiffel is instance based as well.
–
– Jim W.
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?)
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.
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. 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.
David V.