From 0ff2b71c60af348d51526791dcc4166006f4bff8 Mon Sep 17 00:00:00 2001 From: Rahul Bajaj Date: Thu, 3 Aug 2017 02:58:16 +0530 Subject: [PATCH] Chapter2-Organizing Objects With Classes --- .../instance_and_singleton_methods.rb | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Chapter2-Organizing Objects With Classes/instance_and_singleton_methods.rb diff --git a/Chapter2-Organizing Objects With Classes/instance_and_singleton_methods.rb b/Chapter2-Organizing Objects With Classes/instance_and_singleton_methods.rb new file mode 100644 index 0000000..0c41dab --- /dev/null +++ b/Chapter2-Organizing Objects With Classes/instance_and_singleton_methods.rb @@ -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 +