From: Albert Wagner Date: 2001-09-04T10:47:40+09:00 Subject: [ruby-talk:20832] Re: singleton help Hi, Dave. Thanks for the reply. It explains some bizarre behaviour I was getting :-) But, a couple of follow-ups: On Monday 03 September 2001 12:13, you wrote: > Albert Wagner writes: > > I need some help with a singleton class. > > > > 1. With 'new' made private and replaced with 'instance', how do I > > properly pass in the arguments that would normally go to the > > 'initialize(...)' method via 'new'? Just use setter methods? > > 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. > > > 2. If I subclass a singleton class, then create an instance of that > > subclass, do I get the same instance with the additional behaviour of the > > subclass? > > The subclass is it's own singleton: it doesn't share the singleton > parent. Great. That works just as well. As long as the subclassed singleton still inherits instance methods. Thanks again. > > class A > include Singleton > end > class B < A > end > > a1 = A.instance > a2 = A.instance > > b1 = B.instance > b2 = B.instance > > a1 and a2 will be the same, as will b1 and b2. a1 will not be the same > as b1. > > > Regards > > > Dave