-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_modular_code_organization.rb
More file actions
90 lines (67 loc) · 1.79 KB
/
03_modular_code_organization.rb
File metadata and controls
90 lines (67 loc) · 1.79 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
module UsingModuleFunction
module_function
def public_method1
"\n This is a public method one trying to call a private method: #{my_private_method}"
end
def public_method2
"\n This is public method two"
end
private
def my_private_method
"\n my private method is called"
end
end
module ExtendSelfModule
extend self
def public_method1
"\n This is a public method one trying to call a private method: #{my_private_method}"
end
def public_method2
"\n This is public method two"
end
private
def my_private_method
"\n my private method is called"
end
end
class IncludeExtendSelfModule
include ExtendSelfModule
def class_call_module_private
" Class trying to call module's private method #{my_private_method}"
end
end
class IncludeModuleFunction
include UsingModuleFunction
def class_call_module_private
" Class trying to call module's private method #{my_private_method}"
end
end
run_example = "4"
case run_example
###### EXAMPLE 1 #######
when "1"
begin
puts UsingModuleFunction.public_method1
rescue NameError => e
puts "Example 1: UsingModuleFunction"
puts " I expected an error and got it!"
puts " ERROR: #{e.to_s}"
end
###### EXAMPLE 2 #######
when "2"
puts "Example 2: Extending Self"
puts ExtendSelfModule.public_method1
###### EXAMPLE 3 #######
when "3"
puts "Example 3: "
puts IncludeExtendSelfModule.new.public_method1
###### EXAMPLE 4 #######
when "4"
puts "Example 4: "
puts IncludeExtendSelfModule.new.class_call_module_private
puts IncludeExtendSelfModule.new.my_private_method
when "5"
puts "Example 5: "
puts IncludeModuleFunction.new.class_call_module_private
puts IncludeModuleFunction.new.my_private_method
end