forked from skmetz/poodr2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_40.rb
More file actions
101 lines (78 loc) · 1.75 KB
/
Copy path6_40.rb
File metadata and controls
101 lines (78 loc) · 1.75 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
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
# ...
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 spares
{tire_size: tire_size,
chain: chain}
end
def default_chain
"11-speed"
end
def default_tire_size
raise NotImplementedError
end
end
class RoadBike < Bicycle
attr_reader :tape_color
def initialize(**opts)
@tape_color = opts[:tape_color]
super
end
def spares
super.merge(tape_color: tape_color)
end
def default_tire_size
"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
"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
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
class RecumbentBike < Bicycle
attr_reader :flag
def initialize(**opts)
@flag = opts[:flag] # forgot to send `super`
end
def spares
super.merge(flag: flag)
end
def default_chain
'10-speed'
end
def default_tire_size
'28'
end
end
bent = RecumbentBike.new(flag: 'tall and orange')
puts bent.spares
# => {:tire_size=>nil, :chain=>nil, :flag=>"tall and orange"}
# => {:tire_size=>nil, <- didn't get initialized
# => :chain=>nil,
# => :flag=>"tall and orange"}