forked from skmetz/poodr2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_20.rb
More file actions
66 lines (53 loc) · 1.07 KB
/
Copy path2_20.rb
File metadata and controls
66 lines (53 loc) · 1.07 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
class Gear
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
def ratio
@chainring / @cog.to_f # <-- road to ruin
end
end
puts "1st ratio = #{Gear.new(54,11).ratio}"
class Gear
attr_reader :chainring, :cog # <-------
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
def ratio
chainring / cog.to_f # <-------
end
end
puts "2st ratio = #{Gear.new(54,11).ratio}"
# default-ish implementation via attr_reader
def cog
@cog
end
# a simple reimplementation of cog
def cog
@cog * unanticipated_adjustment_factor
end
# a more complex one
def cog
@cog * (foo? ? bar_adjustment : baz_adjustment)
end
class Gear
private
attr_reader :chainring, :cog
public
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
# ...
def ratio
chainring / cog.to_f
end
end
class Blinkered
def cog(gear)
gear.cog
end
end
puts Blinkered.new.cog(Gear.new(54,11))
# => private method `cog' called for #<Gear:0x00007fa4ef926120 @chainring=54, @cog=11>