-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_3.rb
315 lines (265 loc) · 6.56 KB
/
chapter_3.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
############## Page 36 ##############
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@rim = rim
@tire = tire
end
def gear_inches
ratio * Wheel.new(rim, tire).diameter
end
def ratio
chainring / cog.to_f
end
# ...
end
class Wheel
attr_reader :rim, :tire
def initialize(rim, tire)
@rim = rim
@tire = tire
end
def diameter
rim + (tire * 2)
end
# ...
end
Gear.new(52, 11, 26, 1.5).gear_inches
############## Page 39 ##############
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@rim = rim
@tire = tire
end
def gear_inches
ratio * Wheel.new(rim, tire).diameter
end
# ...
end
Gear.new(52, 11, 26, 1.5).gear_inches
############## Page 41 ##############
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, wheel)
@chainring = chainring
@cog = cog
@wheel = wheel
end
def gear_inches
ratio * wheel.diameter
end
# ...
end
# Gear expects a 'Duck' that knows 'diameter'
Gear.new(52, 11, Wheel.new(26, 1.5)).gear_inches
############## Page 43 ##############
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@wheel = Wheel.new(rim, tire)
end
def gear_inches
ratio * wheel.diameter
end
# ...
############## Page 42 ##############
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@rim = rim
@tire = tire
end
def gear_inches
ratio * wheel.diameter
end
def wheel
@wheel ||= Wheel.new(rim, tire)
end
# ...
############## Page 44 ##############
def gear_inches
ratio * wheel.diameter
end
############## Page 44 ##############
def gear_inches
#... a few lines of scary math
foo = some_intermediate_result * wheel.diameter
#... more lines of scary math
end
############## Page 45 ##############
def gear_inches
#... a few lines of scary math
foo = some_intermediate_result * diameter
#... more lines of scary math
end
def diameter
wheel.diameter
end
############## Page 46 ##############
# The point illustrated in this is to remove argument-order dependencies.
# That is, initialize must be run prior to any other methods in order for
# the other methods that are DEPENDENT on them to work properly.
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, wheel)
@chainring = chainring
@cog = cog
@wheel = wheel
end
...
end
Gear.new(
52,
11,
Wheel.new(26, 1.5)).gear_inches
############## Page 47 ##############
# This problem is solved in the below example. This uses the special "args" hash
# and passes in the arguments that need to be initialized.
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(args)
@chainring = args[:chainring]
@cog = args[:cog]
@wheel = args[:wheel]
end
...
end
Gear.new(
:chainring => 52,
:cog => 11,
:wheel => Wheel.new(26, 1.5)).gear_inches
############## Page 48 ##############
# specifying defaults using ||
def initialize(args)
@chainring = args[:chainring] || 40
@cog = args[:cog] || 18
@wheel = args[:wheel]
end
############## Page 49 ##############
# specifying defaults using fetch
# this is the preferred method for setting a default because it anticipates that the element is in the hash and is extant
def initialize(args)
@chainring = args.fetch(:chainring, 40)
@cog = args.fetch(:cog, 18)
@wheel = args[:wheel]
end
############## Page 49 ##############
# specifying defaults by merging a defaults hash
def initialize(args)
args = defaults.merge(args)
@chainring = args[:chainring]
# ...
end
def defaults
{:chainring => 40, :cog => 18}
end
############## Page 50 ##############
# Isolating multiparameter initialization is a tactic that helps when interacting with methods that you do not own
# (and therefore cannot change) and require fixed-order arguments
# When Gear is part of an external interface
module SomeFramework
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, wheel)
@chainring = chainring
@cog = cog
@wheel = wheel
end
# ...
end
end
# wrap the interface to protect yourself from changes
module GearWrapper
def self.gear(args)
SomeFramework::Gear.new(args[:chainring],
args[:cog],
args[:wheel])
end
end
# Now you can create a new Gear using an arguments hash.
GearWrapper.gear(
:chainring => 52,
:cog => 11,
:wheel => Wheel.new(26, 1.5)).gear_inches
# An object that's sole purpose is to create other objects is called a factory.
############## Page ?? ##############
# This is the complete code for example above
module SomeFramework
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, wheel)
@chainring = chainring
@cog = cog
@wheel = wheel
end
def gear_inches
ratio * wheel.diameter
end
def ratio
chainring / cog.to_f
end
end
end
class Wheel
attr_reader :rim, :tire
def initialize(rim, tire)
@rim = rim
@tire = tire
end
def diameter
rim + (tire * 2)
end
end
module GearWrapper
def self.gear(args)
SomeFramework::Gear.new(args[:chainring],
args[:cog],
args[:wheel])
end
end
GearWrapper.gear(
:chainring => 52,
:cog => 11,
:wheel => Wheel.new(26, 1.5)).gear_inches
############## Page 52 ##############
# Dependencies always have a direction - one way to manage this is to reverse the direction.
class Gear
attr_reader :chainring, :cog
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
def gear_inches(diameter)
ratio * diameter
end
def ratio
chainring / cog.to_f
end
# ...
end
class Wheel
attr_reader :rim, :tire, :gear
def initialize(rim, tire, chainring, cog)
@rim = rim
@tire = tire
@gear = Gear.new(chainring, cog)
end
def diameter
rim + (tire * 2)
end
def gear_inches
gear.gear_inches(diameter)
end
# ...
end
Wheel.new(26, 1.5, 52, 11).gear_inches
# IMPORTANT - Depend on things that change less often than you do