[ANN] relative-require v1.0

Z

zimbatm ...

relative-require.rb
===================

This module provides relative requires to the requiring file. This
should solve
most of the uglyness of pushing to $LOAD_PATH and loading
#expand_path-ed files.

Ideally, that would be something I'd like to see in ruby 2.0

`gem install relative-require` or source at
https://github.com/zimbatm/relative-require.rb

Tested with ruby-1.8.7 and ruby-1.9.2

Example project
---------------

This example shows that you don't have to mess with $LOAD_PATH anymore.

$ export RUBYOPT=-relative-require

lib/
* myproject.rb
* myproject/xyz.rb
test/
test.rb

in myproject.rb
require './myproject/xyz.rb'

in test.rb
require '../lib/myproject.rb'

The end
-------

Any feedback or patches welcome !

Cheers,
zimbatm
 
Z

zimbatm ...

Good point. To tell you the truth, it's something that didn't exist in
the 1.8 serie and I didn't really follow the 1.9 development. This is
something that always annoyed me so I decided to tackle the problem.

Still, I believe that there is no need for require_relative (or I didn't
study the problem enough). By using the "./" notation in your require
path, you basically announce that it will be relative. The current
behavior is to be relative to the `pwd`, but most of the time (in tests
or in libraries) the most useful is to be relative to the calling's file
path. In the rare cases where you want to load something from the pwd,
you can fallback to File.join(Dir.pwd, "some-file").
 
U

Urabe Shyouhei

You don't need require_relative because you're doing the same thing as that
does, and vice versa. No need for you library because we already have a
standard method to do it.
 
B

Bob Hutchison

Why don't you use require_relative which is a standard ruby feature?
=20


Is it? It seems to be an extension library. What does that mean anyway? =
And it isn't documented on ruby-doc.org, so...

But it does seem to be awfully similar.

Cheers,
Bob
 
J

James Edward Gray II

On 2010-11-27, at 7:48 AM, Urabe Shyouhei wrote:
=20
=20
Is it?

Yes. It was added in Ruby 1.9.
And it isn't documented on ruby-doc.org, so...

Here's the documentation from the Pickaxe:

Requires a library whose path is relative to the file containing the =
call. Thus, if the directory 1.9 /usr/local/mylib/bin contains the =
file myprog.rb and that program contains the following line:

require_relative "../lib/mylib"

then Ruby will look for mylib in /usr/local/mylib/lib. require_relative =
cannot be called interactively in irb.

James Edward Gray II=
 
Z

zimbatm ...

Sorry for not doing my homework first, I think I will unpublish this
gem. Maybe a backward-compatible require_relative will be more useful.

Just to finish this thread, James and Urabe, do you remember why the
meaning of "./" wasn't simply changed in #require instead of creating a
new method ?
 
B

Bob Hutchison

On Nov 27, 2010, at 11:34 AM, Bob Hutchison wrote:
=20
=20
Yes. It was added in Ruby 1.9.
=20
=20
Here's the documentation from the Pickaxe:

Thanks James. Is there online documentation for Ruby that's complete? I =
know, for a fact, that there's a bunch of experienced Ruby developers =
who've never heard of this particular method. And of course, I'm curious =
what else is in those extensions.

Cheers,
Bob
=20
Requires a library whose path is relative to the file containing the =
call. Thus, if the directory 1.9 /usr/local/mylib/bin contains the =
file myprog.rb and that program contains the following line:
=20
require_relative "../lib/mylib"
=20
then Ruby will look for mylib in /usr/local/mylib/lib. =
require_relative cannot be called interactively in irb.
 
J

James Edward Gray II

On 2010-11-27, at 1:02 PM, James Edward Gray II wrote:
=20
=20
Thanks James. Is there online documentation for Ruby that's complete? =
I know, for a fact, that there's a bunch of experienced Ruby developers =
who've never heard of this particular method. And of course, I'm curious =
what else is in those extensions.

I don't know of a complete online source, no. I still recommend keeping =
a copy of the Pickaxe handy for at least that reason.

James Edward Gray II=
 
B

Bob Hutchison

=20
I don't know of a complete online source, no. I still recommend =
keeping a copy of the Pickaxe handy for at least that reason.

I bought the PDF version of the newest Pickaxe last week. Thanks.

Cheers,
Bob
 
B

Barry Smith

[Note: parts of this message were removed to make it a legal post.]

Any one know how to unsubscribe from getting these emails ??
 

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,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top