-
Notifications
You must be signed in to change notification settings - Fork 1
/
classes.rb
220 lines (170 loc) · 3.48 KB
/
classes.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# class instance (aka object)
my_string = String.new("This is my string")
# initialising classes
class Car
def initialize (colour, model, cylinders)
@colour = colour
@model = model
@cylinders = cylinders
end
end
nissan = Car.new("burgundy", "x-trail", 4)
p nissan
# Getter Methods
class Car
def colour
@colour
end
def model
@model
end
def cylinders
@cylinders
end
end
nissan = Car.new("burgundy", "x-trail", 4)
puts nissan.colour
puts nissan.model
# Setter Methods
class Dog
def legs= number_of_legs
@legs = number_of_legs
end
end
my_dog = Dog.new
my_dog.legs= 3
p my_dog
# Shorted Getter & Setters
class SportsCar
attr_reader :model, :cylinders
attr_accessor :colour
def initialize (colour, model, cylinders)
@colour = colour
@model = model
@cylinders = cylinders
end
end
lamborghini = SportsCar.new("green", "Gallardo LP 560-4", 10)
puts lamborghini.colour
lamborghini.colour = "red"
puts lamborghini.colour
puts lamborghini.model
puts lamborghini.cylinders
# Boolean Methods
class Dog
def can_eat? food
food == "dog food"
end
end
my_dog = Dog.new
puts my_dog.can_eat? "cats"
puts my_dog.can_eat? "dog food"
# bang bang
my_message = "this is my new message"
puts my_message.delete("new ")
puts my_message
my_message.delete!("new ")
puts my_message
# public, private and protected attributes & inheritance
# private & public attributes
class Book
def initialize(wholesale, markup_percentage)
@wholesale = wholesale
@markup_percentage = Float(markup_percentage/100)
end
def can_purchase(available_funds)
calculate_sell_price <= available_funds
end
def calculate_sell_price
markup_amount = @wholesale * @markup_percentage
@wholesale += markup_amount
end
private :calculate_sell_price
end
book = Book.new(45, 25)
puts "you have enough dollars to buy this book" if book.can_purchase(100)
# protected attributes
class Person
attr_reader :age
protected :age
def initialize(age)
@age = age
end
def age_difference_with(other_person)
(self.age - other_person.age).abs
end
end
fred = Person.new 34
chris = Person.new 25
puts chris.age_difference_with fred
#puts chris.age
# inheritance & method overriding
class Person
attr_reader :name
def initialize(name)
@name = name
end
end
class Doctor < Person
def name
"Dr. " + super
end
end
doctor = Doctor.new("Sam Parker")
puts doctor.name
patient = Person.new("Jack James")
puts patient.name
# overriding operators
class Song
def initialize
@plays = 0
end
def <<(play)
@plays += play
self
end
def plays
@plays
end
end
my_song = Song.new
my_song << 10 << 20 << 30
puts my_song.plays
# mix and match with Modules
module Spoon
attr_accessor :capacity
def initialize(capacity)
@capacity = capacity
end
end
module Fork
attr_accessor :tines
def initialize(tines)
@tines = tines
end
end
class Spork
include Spoon
include Fork
def initialize (capacity, tines)
@capacity = capacity
@tines = tines
end
end
utensil = Spork.new("25 mls", 3)
p utensil
# classes & variables
person1_first_name = "Rob"
person2_first_name = person1_first_name
person2_first_name[0] = "B"
puts person1_first_name
puts person2_first_name
person1_first_name = "Rob"
person2_first_name = person1_first_name.dup
person2_first_name[0] = "B"
puts person1_first_name
puts person2_first_name
# stopping modifications
person2_first_name = person1_first_name
person1_first_name.freeze
person2_first_name[0] = "B"