[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Initialization using the Singleton design pattern

Jonathan Leighton

1/5/2006 6:20:00 PM

Hi,

I have a singleton class, as in one that does "include Singleton". I
want to run a few methods when the instance is created. Usually one
would do that in initialize(), but obviously I can't do that here. This
is what I've thought of but it raises an error which says "super: no
superclass method `instance'"

class SingletonClass < ParentClass
include Singleton

def self.instance
super
# initialization code here
end
end

What am I doing wrong?

Cheers

--
Jonathan Leighton
http://turnips... | http://jonathanlei... | http://digital-...



3 Answers

James Gray

1/5/2006 6:48:00 PM

0

On Jan 5, 2006, at 12:19 PM, Jonathan Leighton wrote:

> Hi,
>
> I have a singleton class, as in one that does "include Singleton". I
> want to run a few methods when the instance is created. Usually one
> would do that in initialize(), but obviously I can't do that here.

Why not? The object is still constructed:

>> require "singleton"
=> true
>> class JustOne
>> def initialize
>> puts "initialize() called"
>> end
>> include Singleton
>> end
=> JustOne
>> JustOne.instance
initialize() called
=> #<JustOne:0x32321c>
>> JustOne.instance
=> #<JustOne:0x32321c>

James Edward Gray II



Austin Ziegler

1/5/2006 6:48:00 PM

0

On 05/01/06, Jonathan Leighton <lists@turnipspatch.com> wrote:
> I have a singleton class, as in one that does "include Singleton". I
> want to run a few methods when the instance is created. Usually one
> would do that in initialize(), but obviously I can't do that here. This
> is what I've thought of but it raises an error which says "super: no
> superclass method `instance'"

Right. Just use #initialize as normal, though.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Jonathan Leighton

1/5/2006 9:26:00 PM

0

On Fri, 2006-01-06 at 03:47 +0900, James Edward Gray II wrote:
> On Jan 5, 2006, at 12:19 PM, Jonathan Leighton wrote:
>
> > Hi,
> >
> > I have a singleton class, as in one that does "include Singleton". I
> > want to run a few methods when the instance is created. Usually one
> > would do that in initialize(), but obviously I can't do that here.
>
> Why not? The object is still constructed:
>
> >> require "singleton"
> => true
> >> class JustOne
> >> def initialize
> >> puts "initialize() called"
> >> end
> >> include Singleton
> >> end
> => JustOne
> >> JustOne.instance
> initialize() called
> => #<JustOne:0x32321c>
> >> JustOne.instance
> => #<JustOne:0x32321c>

Okay thanks, I've got it sorted now. I had tried using initialize()
previously but I guess I got put off as what I was doing raised an
exception -- so I assumed you weren't supposed to use initialize()
without thinking about it any harder.

Cheers

--
Jonathan Leighton
http://turnips... | http://jonathanlei... | http://digital-...