forked from skmetz/poodr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchapter_2.rb
243 lines (199 loc) · 4.73 KB
/
chapter_2.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
############## Page 18 ##############
chainring = 52 # number of teeth
cog = 11
ratio = chainring / cog.to_f
puts ratio # -> 4.72727272727273
chainring = 30
cog = 27
ratio = chainring / cog.to_f
puts ratio # -> 1.11111111111111
############## Page 19 ##############
class Gear
attr_reader :chainring, :cog
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
def ratio
chainring / cog.to_f
end
end
puts Gear.new(52, 11).ratio # -> 4.72727272727273
puts Gear.new(30, 27).ratio # -> 1.11111111111111
############## Page 20 ##############
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@rim = rim
@tire = tire
end
def ratio
chainring / cog.to_f
end
def gear_inches
# tire goes around rim twice for diameter
ratio * (rim + (tire * 2))
end
end
puts Gear.new(52, 11, 26, 1.5).gear_inches
# -> 137.090909090909
puts Gear.new(52, 11, 24, 1.25).gear_inches
# -> 125.272727272727
############## Page 20 ##############
puts Gear.new(52, 11).ratio # didn't this used to work?
# ArgumentError: wrong number of arguments (2 for 4)
# from (irb):20:in `initialize'
# from (irb):20:in `new'
# from (irb):20
############## Page 24 ##############
class Gear
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
def ratio
@chainring / @cog.to_f # <-- road to ruin
end
end
############## Page 25 ##############
class Gear
attr_reader :chainring, :cog # <-------
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
def ratio
chainring / cog.to_f # <-------
end
end
############## Page 25 ##############
# default implementation via attr_reader
def cog
@cog
end
############## Page 25 ##############
# a simple reimplementation of cog
def cog
@cog * unanticipated_adjustment_factor
end
############## Page 25 ##############
# a more complex one
def cog
@cog * (foo? ? bar_adjustment : baz_adjustment)
end
############## Page 26 ##############
class ObscuringReferences
attr_reader :data
def initialize(data)
@data = data
end
def diameters
# 0 is rim, 1 is tire
data.collect {|cell|
cell[0] + (cell[1] * 2)}
end
# ... many other methods that index into the array
end
############## Page 27 ##############
# rim and tire sizes (now in milimeters!) in a 2d array
@data = [[622, 20], [622, 23], [559, 30], [559, 40]]
############## Page 28 ##############
class RevealingReferences
attr_reader :wheels
def initialize(data)
@wheels = wheelify(data)
end
def diameters
wheels.collect {|wheel|
wheel.rim + (wheel.tire * 2)}
end
# ... now everyone can send rim/tire to wheel
Wheel = Struct.new(:rim, :tire)
def wheelify(data)
data.collect {|cell|
Wheel.new(cell[0], cell[1])}
end
end
############## Page 29 ##############
def diameters
wheels.collect {|wheel|
wheel.rim + (wheel.tire * 2)}
end
############## Page 29 ##############
# first - iterate over the array
def diameters
wheels.collect {|wheel| diameter(wheel)}
end
# second - calculate diameter of ONE wheel
def diameter(wheel)
wheel.rim + (wheel.tire * 2)
end
############## Page 30 ##############
def gear_inches
# tire goes around rim twice for diameter
ratio * (rim + (tire * 2))
end
############## Page 30 ##############
def gear_inches
ratio * diameter
end
def diameter
rim + (tire * 2)
end
############## Page 32 ##############
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@wheel = Wheel.new(rim, tire)
end
def ratio
chainring / cog.to_f
end
def gear_inches
ratio * wheel.diameter
end
Wheel = Struct.new(:rim, :tire) do
def diameter
rim + (tire * 2)
end
end
end
############## Page 33 ##############
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, wheel=nil)
@chainring = chainring
@cog = cog
@wheel = wheel
end
def ratio
chainring / cog.to_f
end
def gear_inches
ratio * wheel.diameter
end
end
class Wheel
attr_reader :rim, :tire
def initialize(rim, tire)
@rim = rim
@tire = tire
end
def diameter
rim + (tire * 2)
end
def circumference
diameter * Math::PI
end
end
@wheel = Wheel.new(26, 1.5)
puts @wheel.circumference
# -> 91.106186954104
puts Gear.new(52, 11, @wheel).gear_inches
# -> 137.090909090909
puts Gear.new(52, 11).ratio
# -> 4.72727272727273