3 ms·
One use might be to create simple singletons. That said, I'm not entirely sure how you would do it, maybe have `Object#initialize` redefine `Object#new` to alw
by netghost 11y ago
One use might be to create simple singletons. That said, I'm not entirely sure how you would do it, maybe have `Object#initialize` redefine `Object#new` to always return the singleton.
- netghost 11y agoYup, here's how to use it to define a Singleton that is transparent to the caller: class S class << self attr_accessor :singleton end attr_accessor :value def initialize @value = "xxx" S.singleton = self def S.new S.singleton end end end a = S.new # => #<S:0x007ff4a41532e0 @value="xxx"> b = S.new #=> #<S:0x007ff4a41532e0 @value="xxx"> a.value = "zzz" b.value # "zzz" a.object_id == b.object_id #=> true That said... doing this may win you great sorrow.