-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathprivate.rb
74 lines (60 loc) · 1.15 KB
/
private.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Document
def result
"Result is #{show_result}"
end
private
def show_result
50
end
end
class Document
def main_method
method_private
end
private
def method_private
puts "Inside method private for #{self.class}"
end
end
class Html < Document
def main_method
method_private
end
end
class Xml < Document
def main_method
self.method_private #We were trying to call a private method with an explicit receiver and if called in the same class with self would fail.
end
end
Xml.new.main_method
# Protected method
class Document
def main_method
method_protected
end
protected
def method_protected
puts "InSide method_protected for #{self.class}"
end
end
class Json < Document
def main_method
method_protected #called by implicit receiver
end
end
class Xml < Document
def main_method
self.method_protected #called by explicit receiver "an object of the same class"
end
end
class Json < Test1
def main_method
Html.new.method_protected # "Html.new is the same type of object as self"
end
end
Json.new.main_method
class Pdf
def main_method
Json.new.method_protected
end
end