Ruby Singleton Methods and Singleton Classes

Surendran Sukumaran

49 sec read

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).

[source language=”ruby”]
class ClassMethods
def ClassMethods.method1
“Hi I am class method prefaced with classname”
end
def self.method2
“Hi I am class method prefaced with self keyword ”
end
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 # (NoMethodError)
[/source]
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
[source language=”ruby”]
firstObj.singleton_methods => [“singletonMethod”]
secondObj.singleton_methods => []
[/source]
To avoid throwing undefined method error when you try to access a singleton method with some other object
[source language=”ruby”]
if secondObj.singleton_methods.include?(“singletonMethod”)
secondObj.singletonMethod
end
[/source]
An alternative way of checking is
[source language=”ruby”]
if secondObj.singleton_methods.include?( :singletonMethod )
secondObj.singletonMethod
end
[/source]

Singleton classes

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.
[source language=”ruby”]
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.

Related posts:

4 Replies to “Ruby Singleton Methods and Singleton Classes”

  1. To be clear,
    ClassMethods.method1 is a singleton method bound to ClassMethods object.
    self.method2 is also a singleton method since it is bound to ClassMethods class.
    Am I right?
    03.def ClassMethods.method1
    04.”Hi I am class method prefaced with classname”
    05.end
    06.
    07.def self.method2
    08.”Hi I am class method prefaced with self keyword ”
    09.end

  2. A great table showing where methods go; class, singleton class or even singleton class of the singleton class, which is the singleton class of the super class
    http://adprog.blogspot.com/
    another great place to see a graphical representation of the ruby class model is under the documentation for the Class object in ruby docs.

Leave a Reply

Your email address will not be published. Required fields are marked *