Initialization using the Singleton design pattern

  • Thread starter Jonathan Leighton
  • Start date
J

Jonathan Leighton

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
 
J

James Edward Gray II

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:
initialize() called
=> #<JustOne:0x32321c>

James Edward Gray II
 
A

Austin Ziegler

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
 
J

Jonathan Leighton

Why not? The object is still constructed:

initialize() called

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

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

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top