Re: Require stops working

Brian Candler <[email protected]> Fri, 10 Feb 2006 18:27:38 +0000
Newsgroups gmane.comp.apache.mod-ruby
Message-ID <[email protected]>
On Fri, Feb 10, 2006 at 11:15:50AM -0600, Ben Gribaudo wrote:
> Hi,
> 
> I have encountered a problem where the require statement seems to fail 
> after several page loads. Below is a simple set of scripts to 
> demonstrate this issue. I'm using Apache/2.0.54 on Ubuntu Breezy with  
> libapache-ruby1.8 (1.2.4-1), libapache2-mod-ruby (1.2.4-1), liberuby 
> (1.0.5-1).
> 
> == test.rhtml ==
> <%
> require 'test'
> puts "test.rhtml"
> %>
> == test.rb ==
> puts "test.rb"
> == (end of file) ==
> 
> After placing these files in the same directory and viewing test.rhtml 
> in a browser, the expected output is "test.rb test.rhtml". I receive 
> that output for about five page loads. Then, the output changes to just 
> "test.rhtml". It appears that the parser stopped requiring test.rb.

That's correct, this is normal behaviour.

"require" only loads the library if it has not already been loaded. This
state is kept in a global variable, $" or $LOADED_FEATURES

If your Apache server has spawned 5 worker processes, then each will have a
ruby interpreter in it. The first time that particular worker processes
test.rhtml, it will load the library. Once done, that interpreter instance
has it and won't load it again.

If you want to *force* Ruby to load the library on every page hit, use
'load' instead of 'require'

Brian.