-
Notifications
You must be signed in to change notification settings - Fork 43
/
6_46.rb
115 lines (89 loc) · 1.95 KB
/
6_46.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
# ...
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
post_initialize(opts)
end
def spares
{tire_size: tire_size,
chain: chain}.merge(local_spares)
end
def default_tire_size
raise NotImplementedError
end
# subclasses may override
def post_initialize(opts)
end
def local_spares
{}
end
def default_chain
"11-speed"
end
end
class RoadBike < Bicycle
attr_reader :tape_color
def post_initialize(opts)
@tape_color = opts[:tape_color]
end
def local_spares
{tape_color: tape_color}
end
def default_tire_size
"23"
end
end
class MountainBike < Bicycle
attr_reader :front_shock, :rear_shock
def post_initialize(opts)
@front_shock = opts[:front_shock]
@rear_shock = opts[:rear_shock]
end
def local_spares
{front_shock: front_shock}
end
def default_tire_size
"2.1"
end
end
road_bike =
RoadBike.new(
size: 'M',
tape_color: 'red' )
puts road_bike.tire_size # => 23
puts road_bike.chain # => 11-speed
puts road_bike.spares
# => {:tire_size=>"23", :chain=>"11-speed", :tape_color=>"red"}
mountain_bike =
MountainBike.new(
size: 'S',
front_shock: 'Manitou',
rear_shock: 'Fox')
puts mountain_bike.tire_size # => 2.1
puts mountain_bike.chain # => 11-speed
puts mountain_bike.spares
# => {:tire_size=>"2.1", :chain=>"11-speed", :front_shock=>"Manitou"}
class RecumbentBike < Bicycle
attr_reader :flag
def post_initialize(opts)
@flag = opts[:flag]
end
def local_spares
{flag: flag}
end
def default_chain
'10-speed'
end
def default_tire_size
'28'
end
end
bent =
RecumbentBike.new(
size: "M",
flag: 'tall and orange')
puts bent.spares
# => {:tire_size=>"28", :chain=>"10-speed", :flag=>"tall and orange"}