Vegas for the week at a conference. In the Java world, ‘Singleton’ (at least to me) usually referred to the design pattern which allowed you to ensure that there would always only be a maximum of one object instance of a particular class. It was usually implemented by making the constructor private and providing an access method that returned a single (usually static) instance of an singular object. It might looked something like this:
public class SingletonExample { /** * Private class variable */ private static SingletonExample singleton; /** * This is the only method available to access the class variable. * @return the singleton instance */ public static SingletonExample onlyInstance() { if (singleton == null) singleton = new SingletonExample(); return singleton; } /** * The Ctr should be private */ private SingletonExample() { } }
Things appear to be slightly different in the Ruby world. A singleton or eigenclass in the Ruby world refers to a particular instance of an object, and ruby allows us to override the methods and behaviors of a particular object (note – not class).
?> class A >> end => nil >> ?> a = A.new => #<A:0x5ef664> >> b = A.new => #<A:0x5ee1d8> >> ?> class << a >> def to_s; "Instance method"; end >> end => nil >> ?> a => Instance method >> b => #<A:0x5ee1d8>
For more checkout: whytheluckystiff.