Ruby Language Singleton Class

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


  • singleton_class = class << object; self end


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


Got any Ruby Language Question?