# Require within a module: path question

**URL:** <https://www.ruby-forum.com/t/require-within-a-module-path-question/87865>\
**Category:** Ruby\
**Created:** [March 6, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/require-within-a-module-path-question/87865 "2007-03-06T14:59:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [March 6, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/require-within-a-module-path-question/87865/1 "2007-03-06T14:59:12Z")

</div>

All -

It seems to me that if I create a module, a.rb, and a script that  
require’s that module, b.rb, something interesting happens. In this  
case, my a relies on other external files in the project, so I must  
require these files within a.rb. However, I’ve noticed that because b  
requires module a, all the require statements within the file a.rb must  
be relative to b’s location in the directory structure. Thus, when these  
files are stored in different places, it becomes very confusing when  
looking at file a.rb as to why the require statements don’t seem to be  
relative to that file. Is this intended functionality? Have I done  
something stupid?

Thanks,  
Drew

---

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [March 6, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/require-within-a-module-path-question/87865/2 "2007-03-06T15:32:15Z")

</div>

On Tue, Mar 06, 2007 at 10:59:15PM +0900, Drew O. wrote:

> It seems to me that if I create a module, a.rb, and a script that  
> require’s that module, b.rb, something interesting happens. In this  
> case, my a relies on other external files in the project, so I must  
> require these files within a.rb. However, I’ve noticed that because b  
> requires module a, all the require statements within the file a.rb must  
> be relative to b’s location in the directory structure.

No - they just have to be relative to any of the directories in $: (the  
load  
path array)

On my machine I get:

$ ruby1.8 -e ‘puts $:’  
/usr/local/lib/site\_ruby/1.8  
/usr/local/lib/site\_ruby/1.8/i486-linux  
/usr/local/lib/site\_ruby/1.8/i386-linux  
/usr/local/lib/site\_ruby  
/usr/lib/ruby/1.8  
/usr/lib/ruby/1.8/i486-linux  
/usr/lib/ruby/1.8/i386-linux  
.

The last entry (.) means “the current directory”.

So if I do “require ‘a/b’” then the system will try

/usr/local/lib/site\_ruby/1.8/a/b.rb  
/usr/local/lib/site\_ruby/1.8/i486-linux/a/b.rb  
/usr/local/lib/site\_ruby/1.8/i386-linux/a/b.rb  
/usr/local/lib/site\_ruby/a/b.rb  
/usr/lib/ruby/1.8/a/b.rb  
/usr/lib/ruby/1.8/i486-linux/a/b.rb  
/usr/lib/ruby/1.8/i386-linux/a/b.rb  
./a/b.rb

> Thus, when these  
> files are stored in different places, it becomes very confusing when  
> looking at file a.rb as to why the require statements don’t seem to be  
> relative to that file. Is this intended functionality? Have I done  
> something stupid?

If you don’t want to install your own module files in one of the  
locations  
listed above, then install them in some other directory and use

ruby -I /path/to/my/lib a.rb

to run the program; or else at the top of a.rb you can write

$:.unshift("/path/to/my/lib")

In both cases this just adds additional entries to the load path.

HTH,

Brian.
