What is a Singleton method ?
A method which belongs to a single object rather than to an entire class and other objects.
Before explaining about singleton methods I would like to give a small introduction about class methods.
What is a class method ?
When you write your own class methods you do so by prefacing the method name with the name of the class.
There are three ways to write a class method.
- The first way is to preface the class name with the method name(ClassMethods.method1).
- The second way is to preface the self keyword with the method name(self.method2).
- The third way is writing a sepetare class inside the class which contains the methods (class << self).
“Hi I am class method prefaced with classname”
“Hi I am class method prefaced with self keyword ”
class << self def method3 "Hi I am class method inside class withich extends self" end end end [/source] [source language="ruby"] p ClassMethods.method1 # "Hi I am class method prefaced with classname" p ClassMethods.method2 # "Hi I am class method prefaced with self keyword" p ClassMethods.method3 # "Hi I am class method inside a self class" [/source] You can use the similar syntax when creating singleton methods for specific objects. This time you prepend the method name with the name of the object: [source language="ruby"] firstObj = ClassMethods.new secondObj = ClassMethods.new def firstObj.singletonMethod p "Hi am a singlton method available only for firstObj" end [/source] Lets try to call the "singletonMethod" with the firstObj. [source language="ruby"] firstObj.singletonMethod # "Hi am a singlton method available only for firstObj" [/source] Now try calling the method with secondObj [source language="ruby"] secondObj.singletonMethod #undefined method `singletonMethod' for #
We get an undefined method error.
So this “singletonMethod” belongs only to the firstObj object and won’t be available for other objects of this class.
If you try to make the secondObj.singletonMethod Ruby will inform you that singletonMethod is an undefined method.
If you like to find all the singleton methods for a specific object, you can say objectname.singleton_methods
In our case
firstObj.singleton_methods => [“singletonMethod”]
secondObj.singleton_methods => 
To avoid throwing undefined method error when you try to access a singleton method with some other object
An alternative way of checking is
if secondObj.singleton_methods.include?( :singletonMethod )
A singleton class is a class which defines a single object.
In our above example we have two objects firstObj and secondObj.
We can create a separate class for the secondObj instance and that is called as a singleton class.
So as per the definition we created a class for a single object.
class << secondObj def method4 p "Hi am a singlton method available only for secondObj" end end [/source] When you see a class definition with the class keyword followed by two less than symbols that is a Singleton class opened for the object. In our ClassMethods class we had something like class << self which is also a singleton class. In Ruby a class itself is an instance of the Ruby class Let's try something like this [source language="ruby"] ClassMethods.class # Class [/source] Hope you liked this post. Let me know your comments.