# Libraries and requiring other files

**URL:** <https://www.ruby-forum.com/t/libraries-and-requiring-other-files/63577>\
**Category:** Ruby\
**Created:** [June 16, 2006, 10:38am UTC](https://www.ruby-forum.com/t/libraries-and-requiring-other-files/63577 "2006-06-16T10:38:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSJSSSSrgen\_PSTjern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@SSJSSSSrgen\_PSTjern](https://www.ruby-forum.com/u/SSJSSSSrgen_PSTjern)\
**Post date:** [June 16, 2006, 10:38am UTC](https://www.ruby-forum.com/t/libraries-and-requiring-other-files/63577/1 "2006-06-16T10:38:55Z")

</div>

Hi!

I’ve written a library (for the sake of this email, let’s call it  
Mylib). The directory structure for it is like this:

lib/  
|- mylib/  
| |- bar.rb  
| - foo.rb  
|  
- mylib.rb

Now, in the applications, I do “require ‘lib/mylib.rb’” or similar. Then  
mylib.rb does “require ‘mylib/foo.rb’” and “require ‘mylib/bar.rb’”.  
This works fine when I use the **FILE** -idiom (to check if you run  
mylib.rb directly), but from another file, this fails. (Because it looks  
for mylib/foo.rb relative to the cwd of the application requiring  
mylib.rb, which is wrong)

What’s the best approach to solve this?

I could either change $LOAD\_PATH in the library, adding  
File.dirname( **FILE** ) to it, or using File.dirname( **FILE** ) as a part  
of _every_ includepath. (require File.dirname( **FILE** ) + ‘…’)

Other suggestions also welcome. 🙂

---

<div class="post-metadata">

**Author:** ![SSJSSSSrgen\_PSTjern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@SSJSSSSrgen\_PSTjern](https://www.ruby-forum.com/u/SSJSSSSrgen_PSTjern)\
**Post date:** [June 16, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/libraries-and-requiring-other-files/63577/2 "2006-06-16T13:41:00Z")

</div>

“JÃ¸rgen P. TjernÃ¸” wrote:

> Hi!
> 
> I’ve written a library (for the sake of this email, let’s call it  
> Mylib). The directory structure for it is like this:
> 
> lib/  
> |- mylib/  
> | |- bar.rb  
> | - foo.rb  
> |  
> - mylib.rb
> 
> Now, in the applications, I do “require ‘lib/mylib.rb’” or similar. Then  
> mylib.rb does “require ‘mylib/foo.rb’” and “require ‘mylib/bar.rb’”.  
> This works fine when I use the **FILE** -idiom (to check if you run  
> mylib.rb directly), but from another file, this fails. (Because it looks  
> for mylib/foo.rb relative to the cwd of the application requiring  
> mylib.rb, which is wrong)
> 
> What’s the best approach to solve this?
> 
> I could either change $LOAD\_PATH in the library, adding  
> File.dirname( **FILE** ) to it, or using File.dirname( **FILE** ) as a part  
> of _every_ includepath. (require File.dirname( **FILE** ) + ‘…’)
> 
> Other suggestions also welcome. 🙂

I use the same directory structure in my projects. During testing, I  
just add a -Ilib option to the ruby command. That allows me to “require  
‘mylib’” from client code, and mylib will just do “require ‘mylib/bar’”.  
It avoids all the **FILE** hackery.

Of course, if you don’t want to type -Ilib all the time, use Rake to  
setup your unit tests. The default test task in Rake will handle this  
fine.

When you deploy your application, just deploy to to a directory that is  
included in the standard ruby search path. Generally this is somewhere  
in the sitedir directory.

– Jim W.

---

<div class="post-metadata">

**Author:** ![SSJSSSSrgen\_PSTjern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47774/32.png) [@SSJSSSSrgen\_PSTjern](https://www.ruby-forum.com/u/SSJSSSSrgen_PSTjern)\
**Post date:** [June 16, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/libraries-and-requiring-other-files/63577/3 "2006-06-16T14:31:34Z")

</div>

I also use the `-I' option when I need to test scripts. As Jim said, when you deploy your application, make it install into a directory that Ruby will automatically search. A good way to deploy applications is to use`setup.rb’ (just Google for it) or build a gem.
