double singleton?

R

Roger Pack

Hello all.

Noticed this behavior:
=> #<A:0x36d7a60>

A second singleton?

Cheers!
-r
 
B

Brian Candler

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 }
 
7

7stud --

Roger Pack wrote in post #986564:
Hello all.

Noticed this behavior:

=> #<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?
 
R

Ryan Davis

Roger Pack wrote in post #986564:
=20
I looked through the ruby docs and neither Object, Module, Class, nor=20=
Singelton have a method named 'instance'. Where does that come from?

9998 % ri Singleton
=3D Singleton

(from ruby core)
=
--------------------------------------------------------------------------=
----
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 =3D Klass.instance, Klass.instance a =3D=3D b # =3D> true a.new =
#=20
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
# ...
...=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top