Ruby LanguageSingleton Class


Syntax

  • singleton_class = class << object; self end

Remarks

Singleton classes only have one instance: their corresponding object. This can be verified by querying Ruby's ObjectSpace:

instances = ObjectSpace.each_object object.singleton_class

instances.count            # => 1
instances.include? object  # => true

Using <, they can also be verified to be subclasses of the object's actual class:

object.singleton_class < object.class  # => true

References: