Ruby Singleton Pattern

The singleton pattern is probably one of the most used patterns in the development process.

Here I will show you how to do it in Ruby. It's so easy!

Ruby Standard Library has a Singleton Module which implements the singleton pattern.

Just include the singleton module inside your class and that is all!

require 'singleton'

class World
  def say_hello
    puts("Hello World!")
  end
end

And call it like this:

World.instance.say_hello