Skip to content

DamienRobert/tiny_singleton

Repository files navigation

tiny_singleton

Gem Version Ruby test result Build Status

Description

Tiny Singleton is a very simple implementation of the Singleton Design Pattern. Compared to the Singleton module in the stdlib, one does not need to call 'klass.instance' to get access to the singleton object, one can call 'klass.new' as usual. It is thus easier to add the Singleton Pattern to an existing class without changing the way the code calls it.

Features

Very simple implementation, relying only on Module#prepend

Examples

require 'tiny_singleton'

class Foo
  prepend TinySingleton
  def initialize(foo)
    @foo=foo
  end
end

foo=Foo.new("foo")
bar=Foo.new("bar")
foo.object_id == bar.object_id #=> true

Requirements

It needs at least ruby 2.0 since it relies on Module#prepend

Install

~~~ sh
$ gem install tiny_singleton
~~~

Copyright

Copyright © 2015–2017 Damien Robert

MIT License. See LICENSE.txt for details.