-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrubyobjects.rb
More file actions
131 lines (96 loc) · 2.2 KB
/
Copy pathrubyobjects.rb
File metadata and controls
131 lines (96 loc) · 2.2 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class Vehicle
def initialize(vehicle)
@my_vehicle = vehicle
end
end
class Car
def initialize(car, model )
@my_car = car
@wheels = 4
@model_year = model
@lights = 'off'
@turn_signal = "off"
@speed = 0
end
def get_wheels
@wheels
end
def get_model
@model_year
end
def get_lights
@lights
end
def set_lights
@lights == 'off' ? 'on' : 'off'
end
def get_turn_signal
@turn_signal
end
def set_turn_signal
puts 'choose which side to turn'
side = gets.chomp
@turn_signal = side
end
def get_speed
@speed
end
def display
"The car is #{@my_car} it has #{@wheels} wheels and its speed is #{@speed}"
end
def model_type_year
"{#{@my_car}: #{@model_year}}"
end
end
class Tesla < Car
def initialize(car, model )
super(car, model)
@my_tesla = car
@model_year = model
end
def accel
@speed += 10
end
def decel
@speed -= 7
end
end
class Honda < Car
def initialize(car, model)
super(car, model)
@my_honda = car
@model_year = model
end
def accel
@speed += 2
end
def decel
@speed -= 1.25
end
end
class Toyota < Car
def initialize(car,model)
super(car, model)
@my_toyota = car
@model_year = model
end
def accel
@speed += 7
end
def decel
@speed -= 5
end
end
the_car = Toyota.new('toyota', 1984)
# p the_car.get_wheels
# p the_car.get_model
# the_car.accel
# the_car.decel
# p the_car.get_speed
# p the_car.display
# p the_car.class
garage = [Toyota.new('toyota tacoma', 2013), Toyota.new('toyota carolla', 2007),Honda.new('honda crv', 2009), Honda.new('honda accord', 1978), Tesla.new('tesla model T', 2019), Tesla.new('tesla truckla', 2020)]
sorted_garage = garage.sort_by{|elm| elm.get_model}.each {|e| p e.model_type_year}
puts "\n"
sorted_garage = garage.sort_by {|elm| elm.class.to_s}.each {|el| p el.model_type_year}
sorted_garage = garage.sort_by {|e| [e.class.to_s,e.get_model]}.each {|e| p e.model_type_year}