From singleton.rb:
def included(klass)
super
klass.private_class_method :new, :allocate
klass.extend SingletonClassMethods
Singleton.__init__(klass)
end
and a bit earlier:
def __init__(klass)
klass.instance_eval { @__instance__ = nil }
...
Perhaps that should be instead:
klass.instance_eval { @__instance__ = nil unless defined?
@__instance__ }
Or somewhat more obscure, I think this would do:
klass.instance_eval { @__instance__ ||= nil }
···
--
Posted via http://www.ruby-forum.com/.
7stud
(7stud --)
3
Roger Pack wrote in post #986564:
Hello all.
Noticed this behavior:
require 'singleton'
=> true
class A; include Singleton; end
=> A
A.instance
=> #<A:0x3703c38>
I looked through the ruby docs and neither Object, Module, Class, nor
Singelton have a method named 'instance'. Where does that come from?
···
--
Posted via http://www.ruby-forum.com/\.
9998 % ri Singleton
= Singleton
(from ruby core)
···
On Mar 9, 2011, at 14:34 , 7stud -- wrote:
Roger Pack wrote in post #986564:
Hello all.
Noticed this behavior:
require 'singleton'
=> true
class A; include Singleton; end
=> A
A.instance
=> #<A:0x3703c38>
I looked through the ruby docs and neither Object, Module, Class, nor
Singelton have a method named 'instance'. Where does that come from?
------------------------------------------------------------------------------
The Singleton module implements the Singleton pattern.
Usage:
class Klass
include Singleton
# ...
end
* this ensures that only one instance of Klass lets call it ``the instance''
can be created.
a,b = Klass.instance, Klass.instance a == b # => true a.new #
NoMethodError - new is private ...
* ``The instance'' is created at instantiation time, in other words the first
call of Klass.instance(), thus
class OtherKlass
include Singleton
# ...
...