-
Notifications
You must be signed in to change notification settings - Fork 43
/
6_26.rb
54 lines (43 loc) · 971 Bytes
/
6_26.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
class Bicycle
attr_reader :size
def initialize(**opts)
@size = opts[:size]
@chain = opts[:chain]
@tire_size = opts[:tire_size]
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
# ...
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
end
road_bike = RoadBike.new(
size: 'M',
tape_color: 'red' )
mountain_bike = MountainBike.new(
size: 'S',
front_shock: 'Manitou',
rear_shock: 'Fox')
puts road_bike.size
# => M
puts mountain_bike.size
# => M