summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorzverok <zverok.offline@gmail.com>2019-12-21 22:17:35 +0200
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-12-22 23:17:39 +0900
commite568bb5649a93c60aaa51c1e5308054079815bcc (patch)
treed175d01b1b41e3452123c304e477ffb7166f859f /doc
parent03b983d54c17615e36d56d2937a685fc8c3f2cdb (diff)
downloadruby-e568bb5649a93c60aaa51c1e5308054079815bcc.tar.gz
Update private visibility explanation
Diffstat (limited to 'doc')
-rw-r--r--doc/syntax/modules_and_classes.rdoc38
1 files 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:0x0000559c287f27d0>)
+ a.with_renamed # NoMethodError (private method `m' called for #<A:0x0000559c285f8330>)
=== +alias+ and +undef+