load paths unions?

T

Trans

I want to be able to "union" some load paths. Eg. given

lib/facets/more
lib/facets/core

I'd like to be able to do

require 'facets/foo.rb'

And have it search both more/ and core/ for foo.rb.

What's the best way to achieve this?

T.
 
R

Robert Klemme

I want to be able to "union" some load paths. Eg. given

lib/facets/more
lib/facets/core

I'd like to be able to do

require 'facets/foo.rb'

And have it search both more/ and core/ for foo.rb.

Usually you do

require 'facets/foo'

and have a file "xyz.rb" in facets that will link "more/foo.rb" or
"core/foo.rb" depending on where it's located.

You could as well do this: create a file "facets.rb" in the same
directory that holds "facets" (i.e. lib). Inside of that you do

def frequire(*names)
names.each do |name|
%w{more core}.any? do |base|
begin
require "facets/#{base}/#{name}"
true
rescue
false
end
end or raise LoadError, "Did not find #{name}"
end
end

Or something similar.

robert
 
L

Logan Capaldo

I want to be able to "union" some load paths. Eg. given

lib/facets/more
lib/facets/core

I'd like to be able to do

require 'facets/foo.rb'

And have it search both more/ and core/ for foo.rb.

What's the best way to achieve this?

T.

cd facets
for each in ../facets/*/*.rb
do ln -s $each
done
 
T

Trans

Robert said:
Usually you do

require 'facets/foo'

and have a file "xyz.rb" in facets that will link "more/foo.rb" or
"core/foo.rb" depending on where it's located.

Yes, currently I a separate directory facet/ that I build automatically
which contains nothing but require redirections: Eg. facet/foo.rb
contains "require 'facets/core/foo.rb'". But there are a lot of files
and I find it rather wasteful solution --at tlease two requires for
every one. Plus I have to use a slighly different name -- 'facet'
instead of 'facets'.
You could as well do this: create a file "facets.rb" in the same
directory that holds "facets" (i.e. lib). Inside of that you do

def frequire(*names)
names.each do |name|
%w{more core}.any? do |base|
begin
require "facets/#{base}/#{name}"
true
rescue
false
end
end or raise LoadError, "Did not find #{name}"
end
end

I don't want a seprate method, but I have consieder overriding
#require. I just hoping there might be a better clever way. I hear
Python supports .pth files which can redirect whole path reference to
anther location. I wonder if Ruby could support something like this?

T.
 
R

Robert Klemme

Trans said:
I don't want a seprate method, but I have consieder overriding
#require.

I'm not sure I would not go down that road. Gems do it and from what I
read this caused some problems in the past.
I just hoping there might be a better clever way. I hear
Python supports .pth files which can redirect whole path reference to
anther location. I wonder if Ruby could support something like this?

Dunno. How does it work?

robert
 
L

Logan Capaldo

Unfortuately that's not cross-platform.

T.

Make a rake task to do it? But instead of ln -s just spit out

require 'facets/foo/foo.rb'

into a file. Ok it's a hack, but require ain't exactly Java's import
or C#'s using (which is a good thing, IMO).

If you do want this, and you want to do it the "right" (where "right"
meets my definition of "right" :) ) way, I think your best bet is
build a package/module system, and make it clear that what you are
doing is a little smarter than require (aka
load_once_if_you_dont_trick_me_somehow_anyway)

e.g.:

require 'facets/manager'
import 'factes.foo.*'

Or whatever you want the interface to be.


And you can add your .pth type features to this, etc.
 
P

Peña, Botp

fr trans:
# I'd like to be able to do
#=20
# require 'facets/foo.rb'
#=20
# And have it search both more/ and core/ for foo.rb.
#=20
# What's the best way to achieve this?

modify require to accept -r (recursive) option :)
 
N

nobu

Hi,

At Wed, 9 Aug 2006 01:10:11 +0900,
Trans wrote in [ruby-talk:207056]:
I want to be able to "union" some load paths. Eg. given

lib/facets/more
lib/facets/core

I'd like to be able to do

require 'facets/foo.rb'

And have it search both more/ and core/ for foo.rb.

By moving them to
lib/facets/more/facets/foo.rb
lib/facets/core/facets/foo.rb
and appending lib/facets/more and lib/facets/core to $:.
 
T

Trans

Hi,

At Wed, 9 Aug 2006 01:10:11 +0900,
Trans wrote in [ruby-talk:207056]:
I want to be able to "union" some load paths. Eg. given

lib/facets/more
lib/facets/core

I'd like to be able to do

require 'facets/foo.rb'

And have it search both more/ and core/ for foo.rb.

By moving them to
lib/facets/more/facets/foo.rb
lib/facets/core/facets/foo.rb
and appending lib/facets/more and lib/facets/core to $:.

BTW, that is nice solution, even if it is a bit lengthly when it comes
to the path names. Unfortunately Facets has no central require in which
I can append the paths to $:. Maybe it needs to. We'll see.

Thanks,
T.
 

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
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top