James Gray
8/9/2006 4:44:00 PM
On Aug 9, 2006, at 11:26 AM, Jean Nibee 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 Gray II