Python vs Ruby Singleton
I had dug a bit into Singleton Pattern in JavaScript lately and also was playing with some Python and Ruby Singleton Design pattern and asstonisginly they are very simple and one can enjoy reading the code, as so little code helps one to implement this pattern.
First up is python :
# python -m doctest singleton.py class Singleton(object): """ >>> single = Singleton() >>> single #doctest: +ELLIPSIS <hidden.Singleton object at 0x...> >>> one = Singleton() >>> one == single True """ def <strong>new</strong>(clz, *args, **kargs): if not hasattr(clz, '_inst'): clz._inst = super(Singleton, clz).__new__(clz, *args, **kargs) return cls._inst
Ruby turn, yes ruby has stdlib 'singelton', so all one needs to do is :
>> class Spinster >> include Singleton >> end => Spinster >> lady = Spinster.new NoMethodError: private method `new' called for Spinster:Class from (irb):14 >> lady = Spinster.instance => #<Spinster:0x1004a4420>
Hope you had fun, happy hacking!
Further reading :
Recent blog posts
- watir-webdriver web inspector
- gem list to gemfile
- Packing ruby2.0 on debian.
- Made it into The Guinness Book!
- to_h in ruby 2.0
- Filter elements by pattern jQuery.
- Better HTML password fields for mobile ?
- Grayscale image when user offline
- nth-child CSS pseudo-class Christmas colors
- EventEmitter in nodejs