From e568bb5649a93c60aaa51c1e5308054079815bcc Mon Sep 17 00:00:00 2001 From: zverok Date: Sat, 21 Dec 2019 22:17:35 +0200 Subject: [PATCH] Update private visibility explanation --- doc/syntax/modules_and_classes.rdoc | 38 ++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/doc/syntax/modules_and_classes.rdoc b/doc/syntax/modules_and_classes.rdoc index 8fc84d522a..6122f6e08e 100644 --- a/doc/syntax/modules_and_classes.rdoc +++ b/doc/syntax/modules_and_classes.rdoc @@ -190,9 +190,41 @@ Here is an example: b.n b #=> 1 -- m called on defining class a.n b # raises NoMethodError A is not a subclass of B -The third visibility is +private+. A private method may not be called with a -receiver, not even if it equals +self+. If a private method is called with a -receiver other than a literal +self+ a NoMethodError will be raised. +The third visibility is +private+. A private method may only be called from +inside the owner class without a receiver, or with a literal +self+ +as a receiver. If a private method is called with a +receiver other than a literal +self+, a NoMethodError will be raised. + + class A + def without + m + end + + def with_self + self.m + end + + def with_other + A.new.m + end + + def with_renamed + copy = self + copy.m + end + + def m + 1 + end + + private :m + end + + a = A.new + a.without #=> 1 + a.with_self #=> 1 + a.with_other # NoMethodError (private method `m' called for #) + a.with_renamed # NoMethodError (private method `m' called for #) === +alias+ and +undef+