Skip to content

Instantly share code, notes, and snippets.

@netsprout
Created September 22, 2012 06:10
Show Gist options
  • Select an option

  • Save netsprout/3765319 to your computer and use it in GitHub Desktop.

Select an option

Save netsprout/3765319 to your computer and use it in GitHub Desktop.
Single Instance of a class (ex.Logger)
class Logger
def initialize
@log = File.open("logfile.txt", "a")
end
@@instance = Logger.new
def self.instance
return @@instance
end
def log(msg)
@log.puts(msg)
end
private_class_method :new
end
Logger.instance.log('log message 1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment