From: Dave Thomas Date: 2001-09-04T11:21:52+09:00 Subject: [ruby-talk:20833] Re: singleton help Albert Wagner writes: > > There is a logical problem with having 'instance' take parameters > > (which is why is doesn't, I guess :). You could have > > > > o1 = Fred.instance("barney") > > o2 = Fred.instance("wilma") > > > > What would that mean? > > Actually, I do need to pass an argument to the singleton. It needs > access to the class that created it (owner?) I handled it with a > setup(owner) method, but it seems inelegant. But the point there is that although you've created a single instance, but what would it's state be if you passed different values to #instance? For example class A include Singleton def instance(ownerClass) # NOT ALLOWED... @ownerClass = ownerClass end end class X def initialize @single = A.instance(self) end end class Y def initialize @single = A.instance(self) end end What would the value of A.ownerClass be after X.new Y.new If the singleton is specific to a particular class, perhaps nesting it within that class might be a better solution. If it's shared between classes and each wants to pass a different value to the #instance method, then perhaps it isn't a singleton... Regards Dave