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.
Very simple implementation, relying only on Module#prepend
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
It needs at least ruby 2.0
since it relies on Module#prepend
~~~ sh
$ gem install tiny_singleton
~~~
Copyright © 2015–2017 Damien Robert
MIT License. See LICENSE.txt
for details.