# Singleton initilized twice

**URL:** <https://www.ruby-forum.com/t/singleton-initilized-twice/105836>\
**Category:** Ruby\
**Created:** [June 3, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/singleton-initilized-twice/105836 "2007-06-03T18:01:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 3, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/singleton-initilized-twice/105836/1 "2007-06-03T18:01:23Z")

</div>

HI  
I have a little problem here, I am doing a webserver and I put some  
calculations on the singleton’s initialization and call the methods fo  
this class to do some calculations for me (no active record, just  
memory). The problem is the singleton is initilizing every method api  
from webserver I call.  
Can someone explain how can I solve this problem?

[]´s

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 3, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/singleton-initilized-twice/105836/2 "2007-06-03T19:01:33Z")

</div>

On 03.06.2007 17:57, removed\_email\_address@domain.invalid wrote:

> I have a little problem here, I am doing a webserver and I put some  
> calculations on the singleton’s initialization and call the methods fo  
> this class to do some calculations for me (no active record, just  
> memory). The problem is the singleton is initilizing every method api  
> from webserver I call.  
> Can someone explain how can I solve this problem?

Sounds like you are using CGI and thus your singleton is initialized  
once for each CGI process. You need to find another mechanism to hold  
your singleton - either you use FastCGI or mod-ruby or you create a  
server process that is connected to via DRb from your CGI processes.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 3, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/singleton-initilized-twice/105836/3 "2007-06-03T22:47:07Z")

</div>

On 3 jun, 13:59, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> once for each CGI process. You need to find another mechanism to hold  
> your singleton - either you use FastCGI or mod-ruby or you create a  
> server process that is connected to via DRb from your CGI processes.
> 
> Kind regards
> 
> ```
> robert
> 
> ```

Tks, I was doing a hello worl only ehehe, and runing “ruby script/  
server” on the prompt.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 11, 2008, 2:45am UTC](https://www.ruby-forum.com/t/singleton-initilized-twice/105836/4 "2008-01-11T02:45:58Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Tks, I was doing a hello worl only ehehe, and runing “ruby script/  
> server” on the prompt.

Then it sounds like you still have the problem. I’ve noticed the exact  
same condition – a singleton getting initialized twice – and have  
found a solution that works for me. (Sorry this is 10 months late, but  
maybe it will help someone.)

I’m using 1.8.6-p36 on Windows.

In my case, the issue seemed related to the fact that ‘require’ behaves  
badly if you happen to include the same file multiple times using  
different path specifications (e.g., relative vs. absolute vs.  
yet-another-relative). In those cases, the code in the file being  
‘required’ gets executed each time a ‘new’ path formulation is used,  
even though the same physical file is being referenced.

I addressed this by being careful to always File.expand\_path(…) on the  
path before doing a ‘require’, e.g.,

require File.expand\_path(’…/…/some\_file\_with\_a\_singleton\_class.rb’)

instead of

require ‘…/…/some\_file\_with\_a\_singleton\_class.rb’

I should that, in my case, there was a _chance_ the multiple requires  
were being done on different threads. But, offhand, I don’t think they  
were.

So, I guess the moral is: Singleton users, beware the require  
‘relative\_path’
