-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize_object.rb
More file actions
29 lines (22 loc) · 1.02 KB
/
Copy pathinitialize_object.rb
File metadata and controls
29 lines (22 loc) · 1.02 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
# Initialize Object
# In Ruby, the `initialize` method is a special method that is called when a new instance of a class is created. It is used to set up the initial state of the object. The `initialize` method can take parameters, allowing you to customize the initialization process.
class Guitar
def initialize
# every object will print this message when it is created
puts "A new guitar has been created!"
# after that every object will have these instance variables with the same value
@type = "Acoustic"
@wood = "Mahogany"
@strings = 6
@colors = ["Natural", "Sunburst", "Black"]
end
end
# NOTE: when we create a new instance of the Guitar class,
# it will print the first message and second message before the instance variables are created,
# because the initialize method is called when we create a new instance of the class.
# create instances of the Guitar class
guitar_1 = Guitar.new
guitar_2 = Guitar.new
# checking instance variables with inspect method
p guitar_1
p guitar_2