Ruby Language Singleton Class

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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:



Got any Ruby Language Question?