Libraries and requiring other files

Hi!

I've written a library (for the sake of this email, let's call it Mylib). The directory structure for it is like this:

lib/
  >- mylib/
  > >- bar.rb
  > \- foo.rb
  >
  \- mylib.rb

Now, in the applications, I do "require 'lib/mylib.rb'" or similar. Then mylib.rb does "require 'mylib/foo.rb'" and "require 'mylib/bar.rb'". This works fine when I use the __FILE__-idiom (to check if you run mylib.rb directly), but from another file, this fails. (Because it looks for mylib/foo.rb relative to the cwd of the application requiring mylib.rb, which is wrong)

What's the best approach to solve this?

I could either change $LOAD_PATH in the library, adding File.dirname(__FILE__) to it, or using File.dirname(__FILE__) as a part of *every* includepath. (require File.dirname(__FILE__) + '...')

Other suggestions also welcome. :slight_smile:

···

--
Kindest regards / Med vennlig hilsen,
  Jørgen P. Tjernø
  <jorgen@devsoft.no>

"Jørgen P. Tjernø" wrote:

Hi!

I've written a library (for the sake of this email, let's call it
Mylib). The directory structure for it is like this:

lib/
  >- mylib/
  > >- bar.rb
  > \- foo.rb
  >
  \- mylib.rb

Now, in the applications, I do "require 'lib/mylib.rb'" or similar. Then
mylib.rb does "require 'mylib/foo.rb'" and "require 'mylib/bar.rb'".
This works fine when I use the __FILE__-idiom (to check if you run
mylib.rb directly), but from another file, this fails. (Because it looks
for mylib/foo.rb relative to the cwd of the application requiring
mylib.rb, which is wrong)

What's the best approach to solve this?

I could either change $LOAD_PATH in the library, adding
File.dirname(__FILE__) to it, or using File.dirname(__FILE__) as a part
of *every* includepath. (require File.dirname(__FILE__) + '...')

Other suggestions also welcome. :slight_smile:

I use the same directory structure in my projects. During testing, I
just add a -Ilib option to the ruby command. That allows me to "require
'mylib'" from client code, and mylib will just do "require 'mylib/bar'".
It avoids all the __FILE__ hackery.

Of course, if you don't want to type -Ilib all the time, use Rake to
setup your unit tests. The default test task in Rake will handle this
fine.

When you deploy your application, just deploy to to a directory that is
included in the standard ruby search path. Generally this is somewhere
in the sitedir directory.

-- Jim Weirich

···

--
Posted via http://www.ruby-forum.com/\.

I also use the `-I' option when I need to test scripts. As Jim said,
when you deploy your application, make it install into a directory
that Ruby will automatically search. A good way to deploy applications
is to use `setup.rb' (just Google for it) or build a gem.

···

On 6/16/06, "Jørgen P. Tjernø" <jorgen@devsoft.no> wrote:

Hi!

I've written a library (for the sake of this email, let's call it
Mylib). The directory structure for it is like this:

lib/
>- mylib/
> >- bar.rb
> \- foo.rb
>
\- mylib.rb

Now, in the applications, I do "require 'lib/mylib.rb'" or similar. Then
mylib.rb does "require 'mylib/foo.rb'" and "require 'mylib/bar.rb'".
This works fine when I use the __FILE__-idiom (to check if you run
mylib.rb directly), but from another file, this fails. (Because it looks
for mylib/foo.rb relative to the cwd of the application requiring
mylib.rb, which is wrong)

What's the best approach to solve this?

I could either change $LOAD_PATH in the library, adding
File.dirname(__FILE__) to it, or using File.dirname(__FILE__) as a part
of *every* includepath. (require File.dirname(__FILE__) + '...')

Other suggestions also welcome. :slight_smile:

--
Kindest regards / Med vennlig hilsen,
Jørgen P. Tjernø
<jorgen@devsoft.no>

--
Matt