DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on

3 1

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay