Singleton a class : Singletons « Design Patterns « Ruby






Singleton a class


class MyLogger
  private_class_method :new
  @@logger = nil
  def MyLogger.create
    @@logger = new unless @@logger
    @@logger
  end
end

puts MyLogger.create.id
puts MyLogger.create.id

 








Related examples in the same category

1.define class methods by using a class within a class's singleton class like the code
2.Define a singleton method, one that is tied to a single object
3.Classes as Singletons
4.Modules as Singletons
5.include Singleton