-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcar.rb
32 lines (28 loc) · 1.56 KB
/
car.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
class Car
attr :make, :model
# attr_accessor :color # <-------- #
#
def initialize(make, model, color) #
@make = make #
@model = model #
@color = color #
end #
#
def designation #
model + ' ' + make #
end #
#
# GETTER <-------- #
def color #
@color # `attr_accessor` is
end # shorthand for these
# two methods.
# SETTER <-------- #
def color=(new_color) # `attr` is shorthand
@color = new_color # for just the getter.
end #
end
my_car = Car.new("VW", "Rabbit", "White")
puts "My #{my_car.designation} is painted #{my_car.color}." # SETTING THE COLOR
my_car.color = "Red" # USING THE SETTER
puts "My #{my_car.color} #{my_car.model} is faster now."