# Code structure and using "require"

**URL:** <https://www.ruby-forum.com/t/code-structure-and-using-require/64513>\
**Category:** Ruby\
**Created:** [August 9, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/code-structure-and-using-require/64513 "2006-08-09T18:26:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [August 9, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/code-structure-and-using-require/64513/1 "2006-08-09T18:26:09Z")

</div>

Hi

I’m coming from a java world and I’m trying to understand the structure  
of a Ruby project and how 'require’s are resolved.

In java we have this nice package routine where we put our .java files  
into a directory hierarchy that is nice and neat.

Can I do the same in Ruby (I assume yes) but if I do how are “requires”  
resolved when needed and what happend if I move code around the  
hierarchy; are the resolve paths relative or abosolute based on some  
‘root’ directory I"m unaware of.

(Currently to learn and test all my .rb files are in one directory…  
it’s getting messy)

Thanks.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [August 9, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/code-structure-and-using-require/64513/2 "2006-08-09T18:41:09Z")

</div>

On Aug 9, 2006, at 17:26, Jean N. wrote:

> “requires”  
> resolved when needed and what happend if I move code around the  
> hierarchy; are the resolve paths relative or abosolute based on some  
> ‘root’ directory I"m unaware of.
> 
> (Currently to learn and test all my .rb files are in one directory…  
> it’s getting messy)

It’s actually something like Java’s classpath. There are several  
default locations to search, and paths can be relative to any of them

- Ruby will pick the first one it finds.

You can set the paths to be searched in the environment variable  
RUBYLIB, or access/modify it within Ruby using the built-in variable $:

A concrete example:

irb(main):001:0\> $:  
=\> ["/Users/matt/PhD/scripts", “/usr/local/lib/ruby/site\_ruby/1.8”, “/  
usr/local/lib/ruby/site\_ruby/1.8/powerpc-darwin8.4.0”, “/usr/local/  
lib/ruby/site\_ruby”, “/usr/local/lib/ruby/1.8”, “/usr/local/lib/ruby/  
1.8/powerpc-darwin8.4.0”, “.”]

So require ‘foo’ will use the first ‘foo.rb’ it comes across in any  
of these directories.

You can, in a similar way to Java package names, specify  
subdirectories as well. 'require ‘foo/bar’ will look for a ‘foo’  
subdirectory containing ‘bar.rb’

matthew smillie.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [August 9, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/code-structure-and-using-require/64513/3 "2006-08-09T18:44:09Z")

</div>

Jean N. wrote:

> hierarchy; are the resolve paths relative or abosolute based on some  
> ‘root’ directory I"m unaware of.
> 
> (Currently to learn and test all my .rb files are in one directory…  
> it’s getting messy)
> 
> Thanks.

[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/480f0df738d2f17a/?hl=en#](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/480f0df738d2f17a/?hl=en#)

T.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [August 9, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/code-structure-and-using-require/64513/4 "2006-08-09T19:12:20Z")

</div>

Excellent.

Thanks you very much, now off to Modules… they confuse me. 🙂 I’ll read  
them before I ask about them.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [August 9, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/code-structure-and-using-require/64513/5 "2006-08-09T18:50:09Z")

</div>

On Aug 9, 2006, at 11:26 AM, Jean N. wrote:

> Hi
> 
> I’m coming from a java world and I’m trying to understand the  
> structure  
> of a Ruby project and how 'require’s are resolved.

Welcome to Ruby.

I’ll see if I can explain a little. A require searches Ruby’s load  
path with the required path being relative from each location listed  
in there. You can find Ruby’s load path on your box with something  
like:

$ ruby -r pp -e ‘pp $LOAD\_PATH’  
["/usr/local/lib/ruby/site\_ruby/1.8",  
“/usr/local/lib/ruby/site\_ruby/1.8/i686-darwin8.5.2”,  
“/usr/local/lib/ruby/site\_ruby”,  
“/usr/local/lib/ruby/1.8”,  
“/usr/local/lib/ruby/1.8/i686-darwin8.5.2”,  
“.”]

Notice that the last entry is the working directory. This means that  
you can:

require “lib/whatever”

to load whatever.rb in the lib directory inside the current working  
directory.

You can also influence the load path at runtime:

$ ruby -I lib -r pp -e ‘pp $LOAD\_PATH’  
[“lib”,  
“/usr/local/lib/ruby/site\_ruby/1.8”,  
“/usr/local/lib/ruby/site\_ruby/1.8/i686-darwin8.5.2”,  
“/usr/local/lib/ruby/site\_ruby”,  
“/usr/local/lib/ruby/1.8”,  
“/usr/local/lib/ruby/1.8/i686-darwin8.5.2”,  
“.”]

This is commonly used in testing. Assuming we have a standard Ruby  
project structure of something like:

project/  
bin/  
doc/  
example/  
lib/  
test/  
util/

tests are generally run from the project directory with a command like:

$ ruby -I lib:test test/ts\_all.rb

Hope that helps.

James Edward G. II
