-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy path6_20.rb
55 lines (45 loc) · 1.14 KB
/
6_20.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
class Bicycle
# This class is empty except for initialize.
# Code has been moved to RoadBike.
def initialize(**opts)
end
end
class RoadBike < Bicycle
# Now a subclass of Bicycle.
# Contains all code from the old Bicycle class.
attr_reader :size, :tape_color
def initialize(**opts)
@size = opts[:size]
@tape_color = opts[:tape_color]
end
def spares
{ chain: '11-speed',
tire_size: '23',
tape_color: tape_color}
end
# ...
end
class MountainBike < Bicycle
# Still a subclass of Bicycle.
# Code has not changed.
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' )
puts road_bike.size
# => M
mountain_bike = MountainBike.new(
size: 'S',
front_shock: 'Manitou',
rear_shock: 'Fox')
puts mountain_bike.size
# => undefined method `size' for #<MountainBike:0x00007fd68d947288>