What is a singleton class in ruby?

It seems like I'm missing the point or misunderstanding of the value of the singleton class in Ruby. I heard and read about it in different ways - something more complex than others, but I'm more confused about what it is. Is this a class in itself? Is this the reason all objects belong to the class? The concept is fuzzy , but I think it has something to do with why I can even define a class method (class foo; def foo.bar ...).

So: What is the singleton class in Ruby?

+53
oop ruby singleton
Oct 17 '08 at 14:35
source share
4 answers

Firstly, a small definition: the singleton method is a method that is defined for only one 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 class methods (i.e. defined in the class definition). Class methods are single methods in an instance of the Class class - they are not defined in the class definition. Instead, they are defined in a single object class .

 irb(main):009:0> Foo.method_defined? :method1 => true irb(main):010:0> Foo.method_defined? :method2 => false 

You open a singleton object class 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 

Thus, an alternative way to add Singleton methods to an object would be to define them with the 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 short:

  • methods must always belong to a class (or: be instances of a method of a class)
  • ordinary methods relate to the class in which they are defined (i.e. relate to methods of the class instance)
  • class methods are only singleton Class methods
  • Single object methods are not instance methods of an object class; Rather, they are instances of a singleton object class.
+96
Oct 17 '08 at 18:01
source share

Ruby provides a way to define methods specific to a particular object, and such methods are known as Singleton methods. When someone declares a singleton method for an object, Ruby automatically creates a class to store only singleton methods. The newly created class is called the 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 a list of names for all singleton methods for the object.

  foo.singleton_methods # => [:size] bar.singleton_methods # => [] 

This article really helped me understand Singleton Classes in Ruby, and it has a good code example.

+14
Jan 19 '12 at 20:21
source share

The most pragmatic / action-oriented way to think about it (IMHO): - as an inheritance chain, or the search / resolution order of a method. This image may help.

http://www.klankboomklang.com/2007/11/25/modules-part-i-enter-the-include-class/

This is r 1.9, contrasting the built-in and user-defined classes: I'm still digesting this file.

http://d.hatena.ne.jp/sumim/20080111/p1

Also, I find the confusing use of the term "Singleton object" to be a different concept. The singleton object comes from a class that has its constructor / instantiator method overridden, so you can only select one from this class.

+2
Oct 17 '08 at 16:49
source share

As soon as updating to @Pistos answer, from version 1.9.2 ruby ​​add new syntax to get singleton class

  singleton_class = ( class << foo; self; end ) 

can be replaced by:

 singleton_class = foo.singleton_class 

https://apidock.com/ruby/Object/singleton_class

0
Oct 24 '17 at 17:42 on
source share



All Articles