# Requiring /lib files in a cross-platform way

**URL:** <https://www.ruby-forum.com/t/requiring-lib-files-in-a-cross-platform-way/62199>\
**Category:** Rails\
**Created:** [June 1, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/requiring-lib-files-in-a-cross-platform-way/62199 "2006-06-01T12:38:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [June 1, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/requiring-lib-files-in-a-cross-platform-way/62199/1 "2006-06-01T12:38:27Z")

</div>

Hi.

When you have library foo.rb in your Rails project /lib directory, I  
notice that on some platforms - e.g. Windows with WEBrick as a server

- you can require it by simply:

require ‘lib/foo’

However on other platforms - e.g. Debian with Lighttpd server via  
FastCGI - this does not work, and you need to specify RAILS\_ROOT  
explicitly, like this:

require “#{RAILS\_ROOT}/lib/foo”

So what would you consider the canonical way of requiring /lib files  
within Rails in a sensible, cross-platform way? Would it be the  
second method, or is there in usage some third methods that I’ve  
missed?

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [June 1, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/requiring-lib-files-in-a-cross-platform-way/62199/2 "2006-06-01T12:56:58Z")

</div>

if you have something like:

require\_dependency ‘foo’

in your environment.rb file it picks it up from lib automatically.

Michael

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [June 1, 2006, 8:10pm UTC](https://www.ruby-forum.com/t/requiring-lib-files-in-a-cross-platform-way/62199/3 "2006-06-01T20:10:54Z")

</div>

On 6/1/06, Michael T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Indeed.

I looked into the source of the problem:

The cause is that if you start WEBrick from RAILS\_ROOT, the working  
directory (Dir.pwd) of at least environment.rb (and it seems likely -  
the rest of the application as well) would be RAILS\_ROOT. So when you

require ‘lib/foo’

Ruby would check for ./lib/foo.rb, because . is always in its  
$LOAD\_PATH. Now since ‘.’ is RAILS\_ROOT, it would find  
RAILS\_ROOT/lib/foo.rb, which is what we wanted, so everything’s great.

However, if you load your application through FastCGI (I have checked  
this for Lighttpd, but suspect Apache would give similar result) your  
working directory shall be that of dispatch.fcgi, i.e. /public. Now  
‘.’ is /public, and RAILS\_ROOT is nowhere in the $LOAD\_PATH if you  
haven’t explicitly put it there. So trying to require ‘lib/foo’ would  
fail, or at any rate won’t find the desired RAILS\_ROOT/lib file.

require\_dependency solves this, and enables sensible, cross-platform  
requirement of attached libraries. It’s nicer than the working  
#{RAILS\_ROOT} solution, and also has some additional neat features  
[1]. Thanks for notifying me about it.

–  
-Alder

[1] [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/RequireDependency)

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [June 2, 2006, 2:35am UTC](https://www.ruby-forum.com/t/requiring-lib-files-in-a-cross-platform-way/62199/4 "2006-06-02T02:35:02Z")

</div>

Wow, thanks for the investigation. It’s good to know.

Michael
