-
Notifications
You must be signed in to change notification settings - Fork 43
/
6_37.rb
68 lines (56 loc) · 1.32 KB
/
6_37.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
class Bicycle
attr_reader :size, :chain, :tire_size
def initialize(**opts)
@size = opts[:size]
@chain = opts[:chain] || default_chain
@tire_size = opts[:tire_size] || default_tire_size
end
def default_chain # <- common default
"11-speed"
end
# ...
def default_tire_size
raise NotImplementedError,
"#{self.class} should have implemented..."
end
end
class RoadBike < Bicycle
attr_reader :tape_color
def initialize(**opts)
@tape_color = opts[:tape_color]
super
end
def spares
{ chain: '11-speed',
tire_size: '23',
tape_color: tape_color}
end
def default_tire_size # <- subclass default
"23"
end
# ...
end
class MountainBike < Bicycle
attr_reader :front_shock, :rear_shock
def initialize(**opts)
@front_shock = opts[:front_shock]
@rear_shock = opts[:rear_shock]
super
end
def spares
super.merge(front_shock: front_shock)
end
def default_tire_size # <- subclass default
"2.1"
end
# ...
end
class RecumbentBike < Bicycle
def default_chain
'10-speed'
end
end
bent = RecumbentBike.new(size: "L")
# => RecumbentBike should have implemented...
# => .../some_file.rb:15:in `default_tire_size'
# => /Users/skm/Projects/books/poodr2code/raw/6_37.rb:15:in `default_tire_size'