-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance_method.rb
More file actions
33 lines (27 loc) · 880 Bytes
/
Copy pathinstance_method.rb
File metadata and controls
33 lines (27 loc) · 880 Bytes
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
# instance method
# An instance method is a method that is defined within a class and can be called on instances of that class.
# It is used to perform actions or retrieve information specific to an instance of the class.
# Instance methods can access and modify the instance variables/objects of the object they are called on.
class Guitar
# This is instance variable
def initialize
@type = "Acoustic"
@wood = "Alder"
@strings = 6
end
# This is instance method
def information
puts "This guitar is a #{@type} guitar made of #{@wood} wood with #{@strings} strings."
end
def play
puts "Strumming the #{@type} guitar!"
end
end
# create instances of the Guitar class
guitar_1 = Guitar.new
guitar_2 = Guitar.new
# use instance method on the instances of the Guitar class
guitar_1.information
guitar_1.play
guitar_2.information
guitar_2.play