-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclasses.rb
More file actions
154 lines (109 loc) · 2.94 KB
/
classes.rb
File metadata and controls
154 lines (109 loc) · 2.94 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require_relative 'helpers'
header('Classes')
class Spaceship
def initialize
@creation_date = Time.now.strftime("%d/%m/%Y %H:%M:%S")
end
def launch(destination)
@destination = destination
end
def self.my_class_method
'My class method'
end
def self.my_second_class_method
'My second class method'
end
private_class_method :my_second_class_method
def my_private_method
'My private method'
end
private :my_private_method
private
def second_private_method
'Second private method'
end
protected
def my_protected_method
'My protected method'
end
end
class SpritelySpaceship < Spaceship
def test_protected_method
my_protected_method
end
end
#################################################
spaceship = Spaceship.new
#################################################
headline('Inspect element')
spaceship.launch('Earth')
puts spaceship.inspect #<Spaceship:0x000000016972a0 @creation_date="26/03/2016 13:35:21", @destination="Earth">
p spaceship
headline('Accessing private methods')
# Private methods are not accessible
begin
puts spaceship.my_private_method
rescue NoMethodError => ex
puts ex.message # private method `my_private_method' called for #<Spaceship:0x00000001681b58>
end
begin
puts spaceship.second_private_method
rescue NoMethodError => ex
puts ex.message # private method `second_private_method' called for #<Spaceship:0x000000012f0f48>
end
# But I can call private method by using .send
puts spaceship.send(:my_private_method)
headline('Class methods')
puts Spaceship.my_class_method
headline('Private class methods')
# Private class methods are not accessible
begin
puts Spaceship.my_second_class_method
rescue NoMethodError => ex
puts ex.message # private method `my_second_class_method' called for Spaceship:Class
end
# But I can call a private class method by using .send
puts Spaceship.send(:my_second_class_method)
headline('Protected methods')
# Protected methods are only accesible for objects of the same class
puts SpritelySpaceship.new.test_protected_method
# Protected methods are not accessible
begin
puts spaceship.my_protected_method
rescue NoMethodError => ex
puts ex.message # protected method `my_protected_method' called for #<Spaceship:0x000000021f4f58
end
# But I can call a protected method by using .send
puts spaceship.send(:my_protected_method)
headline('Open classes')
class Car
def wheels
4
end
end
car = Car.new
puts car.wheels
begin
puts car.how_many_wheels
rescue NoMethodError => ex
puts ex.message # undefined method `how_many_wheels' for #<Car:0x000000015bd410>
end
separator
# open classes
class Car
def how_many_wheels
"#{wheels} in total"
end
end
second_car = Car.new
puts second_car.wheels
puts second_car.how_many_wheels
separator
# Monkey patching - Method override
class Car
def how_many_wheels
"#{wheels} I guess"
end
end
third_car = Car.new
puts third_car.how_many_wheels