-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhead_first_ruby3.rb
168 lines (115 loc) · 4.22 KB
/
head_first_ruby3.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#Inheritance
#use inheritance to let your classes share methods
#if one class has some functionality, classes that inherit from it can get that functionality
#automatically
#instead of repeating method definitions across many similar classes, inheritance
#let's you move the common methods to a single class, can then specify that oher classes inherit from this class
#superclass- class with the common methods (e.g. Vehicle)
#subclasses- classes that inherit those methods (e.g. Car, Truck, Motorcycle)
#if a superclass has instance methods- subclasses automaticallly inherit those methods
#can get access to all the methods you need from the superclass, without having to duplicate
#the methods' code in each subclass-which means less duplicated code
#all the classes will still have the same methods, but there's only one copy of each method to maintain!
#note: subclasses technically do not inherit instance variables; they inherit the attributor accessor methods that create those variables
#Defining a superclass (requires nothing special)
#no special syntax to define a superclass, it's just an ordinary class
class Vehicle
attr_accessor :odometer
attr_accessor :gas_used
#all attributes will be inherited when we delcare a subclass
def accelerate
puts "Floor it!"
end
def sound_horn
puts "Beep! Beep!"
end
def steer
puts "Turn front 2 wheels"
end
def mileage
return @odometer / gas_used
end
#all instance methods will also be inherited once subclasses are declared
end
#Defining a subclass
#subclass definition looks just like an ordinary class definition, except that you specify the superclass it will inherit from
#use the less-than < symbol as subclass is a subset of the superclass it will inherit from
#class car < vehicle
#class name #superclass name
#can also define additional methods & attributes here
class Car < Vehicle
end
class Truck < Vehicle
end
class Motorcycle < Vehicle
end
#have access to all of the superclass's functionality
truck = Truck.new
truck.accelerate
truck.steer
car = Car.new
car.odomoter = 11432
car.gas_used = 366
puts "Lifetime MPG:"
puts car.mileage
#Adding methods to subclasses
class Truck < Vehicle
attr_accessor :cargo
def load_bed (contents)
puts "Securing #{contents} in the truck bed"
@cargo = contents
end
end
truck = Truck.new
truck.load_bed("259 bouncy balls")
puts "The truck is carrying #{truck.cargo}"
#subclasses keep inherited methods alongside new ones
#Exercises
class Kite
attr_accessor :fly
attr_accessor :land
def fly (airline)
puts "You're flying with #{airline}!"
@fly = airline
end
def land (airport)
puts "You're landing at #{airport}!"
@land = airport
end
end
class Stuntkite < Kite
attr_accessor :steer
def steer (pilot)
puts "Please steer carefully #{pilot}"
@steer = pilot
end
end
#instance variables belong to the object not the class!
car = car.new
puts car.instance_variables
#testing this there are no instance variables
car.odomoter = 22914
car.gas_used = 728
puts car.instance_variables
#car inherited the instance methods not the instance variables
#Instance variables belong to the object, not the class!
#important becuase if you deviate from your instance variable names matching your accessor method names then the subclass can interfere with its superclass functionality by overriding its instance variables.
#if instance variables are the same then they can write over one another
#use sensible variables names that match your attribute accessor names
#Overriding methods
#If the superclass's behaviour isn't what you need in the subclass, inheritance gives you another mechanism to help: method overriding. Replaces one or more methods in a subclass, replace the inherited methods from the superclass with methods specific to the subclass.
class Motorcycle < Vehicle
def steer
puts "Turn front wheel."
end
end
motorcyle.steer #Turn front wheel
#if we call any other methods on motorcyle we'll get the inherited mehod, if a subclass needs specialised behaviour it can simply override the method it inherited from the superclass.
#subclass of a subclass can be possible- but can become overly complex
class Car < Vehicle
end
class DragRacer < Car
def accelerate
puts "Inject nitrous!"
end
end