Code structure and using "require"

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.

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.

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#

T.

Excellent.

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs