DEV Community

n350071๐Ÿ‡ฏ๐Ÿ‡ต
n350071๐Ÿ‡ฏ๐Ÿ‡ต

Posted on

Singleton module (Singleton class)

๐Ÿ‘ You can call it from anywhere in the app

I've heard that this is a better way because the module can't make an instance. So, your intent is more clear.

module Logger
  mattr_accessor :filename

  def self.log(msg)
    CSV.open(@@filename, "a") do |csv|
      csv << msg
    end
  end
end

Logger.filename = 'db/script/sample_log.csv'
Logger.log('hello')
Logger.log('hey')

Top comments (0)