forked from rabajaj0509/Ruby-concepts-step-by-step
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter2-Organizing Objects With Classes
- Loading branch information
1 parent
e4a545b
commit 0ff2b71
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
Chapter2-Organizing Objects With Classes/instance_and_singleton_methods.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#Methods that are defined inside a class and intented for use by | ||
#all instances of class, are called as instance methods | ||
|
||
#Methods that are defined for one particular object, as in | ||
# def ticket.price are called singleton methods. | ||
|
||
class Ticket | ||
#instance method | ||
def event | ||
puts "This event can apply to all instances of the ticket class" | ||
end | ||
end | ||
|
||
ticket = Ticket.new | ||
ticket.event | ||
|
||
puts "=========================================================================" | ||
|
||
bobs_ticket = Ticket.new | ||
|
||
#Singleton method | ||
def bobs_ticket.price | ||
puts "This event can only be applied to the instance of object named 'ticket'" | ||
end | ||
bobs_ticket.price | ||
|
||
puts "=========================================================================" | ||
|
||
#If you try to use the singleton method on a different instance, it will show you error | ||
ticket_instance = Ticket.new | ||
ticket_instance.price | ||
|