-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinheritance_example.rb
79 lines (58 loc) · 1.29 KB
/
inheritance_example.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
class Vehicle
attr_reader :speed, :direction
attr_writer :speed, :direction
def initialize(input_options)
@speed = input_options[:speed]
@direction = input_options[:direction]
end
def brake
@speed = 0
end
# def accelerate
# @speed += 10
# end
def turn(new_direction)
@direction = new_direction
end
end
class Car < Vehicle
attr_reader :honk_horn, :fuel_range, :make, :model
attr_writer :honk_horn, :fuel_range, :make, :model
def initialize(input_options)
super
@fuel_range = input_options[:fuel_range]
@make = input_options[:make]
@model = input_options[:model]
end
def honk_horn
puts "Beeeeeeep!"
end
end
class Bike < Vehicle
attr_reader :type, :weight
attr_writer :type, :weight
def initialize(input_options)
super
@type = input_options[:type]
@weight = input_options[:weight]
end
def ring_bell
puts "Ring ring!"
end
# def speed
# @speed
# end
end
bike = Bike.new({:type => "schwinn", :weight => 10, :speed => 5, :direction => "South"})
car = Car.new({:fuel_range => 30, :make => "Chevy", :model => "Bolt"})
p bike.type
p bike.weight
p bike.direction
p car.fuel_range
p car.make
p car.model
# bike.accelerate
p bike
# car.accelerate
horn_bike = bike.ring_bell
horn_car = car.honk_horn