Modules as Singletons : Singletons « Design Patterns « Ruby






Modules as Singletons


module ModuleBasedLogger
  ERROR = 1
  WARNING = 2
  INFO = 3

  @@log = File.open("log.txt", "w")
  @@level = WARNING

  def self.error(msg)
    @@log.puts(msg)
    @@log.flush
  end
end

ModuleBasedLogger.error('Computer wins chess game.')

 








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.Singleton a class
4.Classes as Singletons
5.include Singleton