Is the singleton class in Ruby a class in and of itself? Is it the reason why all objects belong to "class?" The concept is fuzzy, but I believe it has something to do with why I can define a class method at all (class foo; def foo.bar ...
).
What is the singleton class in Ruby?
A singleton class of an object (or a class) is a class created by Ruby only for this specific object. This class is somehow “hidden” to us, but it is there. When calling a method on this object, Ruby will look first into its singleton class, if there is one, to find that method.
In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time. After the first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created.
The Singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields.
For example, there is a license with us, and we have only one database connection or suppose if our JDBC driver does not allow us to do multithreading, then Singleton class comes into the picture and makes sure that at a time, only a single connection or a single thread can access the connection.
First, a little definition: a singleton method is a method that is defined only for a single object. Example:
irb(main):001:0> class Foo; def method1; puts 1; end; end => nil irb(main):002:0> foo = Foo.new => #<Foo:0xb79fa724> irb(main):003:0> def foo.method2; puts 2; end => nil irb(main):004:0> foo.method1 1 => nil irb(main):005:0> foo.method2 2 => nil irb(main):006:0> other_foo = Foo.new => #<Foo:0xb79f0ef4> irb(main):007:0> other_foo.method1 1 => nil irb(main):008:0> other_foo.method2 NoMethodError: undefined method `method2' for #<Foo:0xb79f0ef4> from (irb):8
Instance methods are methods of a class (i.e. defined in the class's definition). Class methods are singleton methods on the Class
instance of a class -- they are not defined in the class's definition. Instead, they are defined on the singleton class of the object.
irb(main):009:0> Foo.method_defined? :method1 => true irb(main):010:0> Foo.method_defined? :method2 => false
You open the singleton class of an object with the syntax class << obj
. Here, we see that this singleton class is where the singleton methods are defined:
irb(main):012:0> singleton_class = ( class << foo; self; end ) => #<Class:#<Foo:0xb79fa724>> irb(main):013:0> singleton_class.method_defined? :method1 => true irb(main):014:0> singleton_class.method_defined? :method2 => true irb(main):015:0> other_singleton_class = ( class << other_foo; self; end ) => #<Class:#<Foo:0xb79f0ef4>> irb(main):016:0> other_singleton_class.method_defined? :method1 => true irb(main):017:0> other_singleton_class.method_defined? :method2 => false
So an alternative means of adding singleton methods to an object would be to define them with the object's singleton class open:
irb(main):018:0> class << foo; def method3; puts 3; end; end => nil irb(main):019:0> foo.method3 3 => nil irb(main):022:0> Foo.method_defined? :method3 => false
In summary:
Class
Ruby provides a way to define methods that are specific to a particular object and such methods are known as Singleton Methods. When one declares a singleton method on an object, Ruby automatically creates a class to hold just the singleton methods. The newly created class is called Singleton Class.
foo = Array.new def foo.size "Hello World!" end foo.size # => "Hello World!" foo.class # => Array #Create another instance of Array Class and call size method on it bar = Array.new bar.size # => 0
Singleton class is object specific anonymous class that is automatically created and inserted into the inheritance hierarchy. singleton_methods
can be called on an object to get the list of of names for all of the singleton methods on an object.
foo.singleton_methods # => [:size] bar.singleton_methods # => []
This article really helped me to understand Singleton Classes in Ruby and it has a good code example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With