Code structure and using "require"

J

Jean Nibee

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.
 
M

Matthew Smillie

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)

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.
 
T

Trans

Jean said:
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.

http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/480f0df738d2f17a/?hl=en#

T.
 
J

James Edward Gray II

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
 
J

Jean Nibee

Excellent.

Thanks you very much, now off to Modules.. they confuse me. :) I'll read
them before I ask about them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top