I have singleton class I want to observe with another class. The
problem is that the singleton class will create the instance of the
class that wants to observe it.
Here is an example:
# register for A updates
A.instance.add_observer(self) # will fail, because it waits for A
You need to stop the program with Control-C and you get:
sleep': Interrupt from /usr/lib/ruby/1.8/singleton.rb:149:in_instantiate?’
instance' from c.rb:21:ininitialize’
new' from c.rb:10:ininitialize’
new' from /usr/lib/ruby/1.8/singleton.rb:94:ininstance’
The problem is that A didn’t finish initializing, and B in order to
add_observer waits for A to finish initialize. So both A and B
sleeping waiting for each other.
I thought to solve it by creating a thread that will wait for A to
Something like this:
Thread.new(self) do |me|
while A.instance.not_initialize? do
But how do I monitor if A finished initialize?
Also, is there a better way of doing this? Idiom?