-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy path2_30.rb
75 lines (61 loc) · 1.52 KB
/
2_30.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
class RevealingReferences
attr_reader :wheels
def initialize(data)
@wheels = wheelify(data)
end
# 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
# ... 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
@data = [[622, 20], [622, 23], [559, 30], [559, 40]]
puts "RevealingReferences diameters = #{RevealingReferences.new(@data).diameters}"
# gear_inches does too much
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 "1st Gear gear_inches = #{Gear.new(54,11,622,20).gear_inches}"
# diameter method doesn't belong in Gear, notice it
# depends on rim and tire only
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
ratio * diameter
end
def diameter
rim + (tire * 2)
end
end
puts "2nd Gear gear_inches = #{Gear.new(54,11,622,20).gear_inches}"