# Infinite loop with Singleton mixin

**URL:** <https://www.ruby-forum.com/t/infinite-loop-with-singleton-mixin/161292>\
**Category:** Ruby\
**Created:** [February 25, 2009, 5:46am UTC](https://www.ruby-forum.com/t/infinite-loop-with-singleton-mixin/161292 "2009-02-25T05:46:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 25, 2009, 5:46am UTC](https://www.ruby-forum.com/t/infinite-loop-with-singleton-mixin/161292/1 "2009-02-25T05:46:26Z")

</div>

This loops inifitely, but it doesn’t seem like it should.

–  
require ‘singleton’

class SingleThing  
include Singleton  
def initialize  
@a = OtherThing.new  
end  
end

class OtherThing  
def initialize  
@single\_ref = SingleThing.instance  
end  
end

a = SingleThing.instance

* * *

It looks as if the the SingleThing.instance method doesn’t start  
returning the allocated instance until initialize has _finished_, but it  
seems to me it should start returning the allocated instance as soon as  
initialize _starts_. Otherwise, methods called during initialize (and  
the methods they call, and so on) can’t refer back to the singleton  
object using SingleThing.instance.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 25, 2009, 8:03am UTC](https://www.ruby-forum.com/t/infinite-loop-with-singleton-mixin/161292/2 "2009-02-25T08:03:16Z")

</div>

Hi,

In message “Re: Infinite loop with Singleton mixin”  
on Wed, 25 Feb 2009 13:45:41 +0900, Adam G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|This loops inifitely, but it doesn’t seem like it should.

Uninitialized singleton object should not be disclosed, I think. So I  
think it should loop infinitely, as you specified.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 25, 2009, 6:38pm UTC](https://www.ruby-forum.com/t/infinite-loop-with-singleton-mixin/161292/3 "2009-02-25T18:38:11Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: Infinite loop with Singleton mixin”  
> on Wed, 25 Feb 2009 13:45:41 +0900, Adam G.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> |This loops inifitely, but it doesn’t seem like it should.
> 
> Uninitialized singleton object should not be disclosed, I think. So I  
> think it should loop infinitely, as you specified.
> 
> ```
> matz.
> 
> ```

Personally, I disagree. I think both consistency and utility would be  
better served by having .instance return the instance as soon as  
initialize has started - after all, there are plenty of other ways to  
get references to objects which haven’t finished their initialize  
function, and they don’t cause undue trouble. But I can understand why  
it might need to be the way it is. I’ll try and work around it.

Incidentally, it turns out the code above isn’t looping the way I  
thought it was; it’s actually stuck inside the while loop in  
singleton.rb:148-152

while false.equal?(@ **instance** )  
Thread.critical = false  
sleep(0.08) # timeout  
Thread.critical = true  
end

Which, itself, suggests a work-around:

* * *

require ‘singleton’

class SingleThing  
include Singleton  
def initialize  
self.class.instance\_variable\_set(:@ **instance** ,self)  
@a = OtherThing.new  
end  
end

class OtherThing  
def initialize  
@single\_ref = SingleThing.instance  
end  
end

## a = SingleThing.instance

Anyway, thanks for your attention, and thank you for your hard work on  
Ruby.
