[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: "require and subdirectories" problem

Gavin Sinclair

9/9/2003 2:37:00 PM

On Tuesday, September 9, 2003, 11:20:27 PM, Recheis wrote:

> hi,
> consider a script foo.rb that contains the line

> require './lib/lib.rb'

> lib.rb contains the line:

> require './lib/annother.rb'

> it works.
> (require 'annother.rb' doesn't work allthough the file
> annother.rb is placed in the same directory as lib.rb)

> but i am not very pleased with it. the library in the directory lib,
> must not know from where it is required. what can i do so that lib
> can require all it's files no matter where the script that requires
> lib.rb is located?


I think the best thing you can do is put some effort into your code
organisation so that all "requires" assume a common base. This is not
very easy as Ruby has no enforced standards, but it's not too
difficult once you know how. See
http://www.rubygarden.org/ruby?ProjectDirector...
for some ideas. I should update that soon as I have learnt a lot
more.

To learn the same, play with install.rb from the RAA (project name
"setup" IIRC) and see how another project does it.

Basically, your "requires" should look like this, if your project name
is "foo":

require "foo/one"
require "foo/another"

To achieve this, your project will be installed at <site_ruby>/1.8/foo
and your development directory structure will look like this:

foo/lib/foo/one.rb
foo/lib/foo/another.rb

There are remaining issues, but that's enough for now; you may not
even want to separate development and installation at the moment.

It is abnormal to (a) use filesystem paths and (b) explicitly state
the "rb" extension, so I guess you have a lot to learn... :)

Cheers,
Gavin