Load paths unions?

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.

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

···

On 08.08.2006 18:06, Trans wrote:

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.

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

···

On Aug 8, 2006, at 12:10 PM, Trans wrote:

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.

fr trans:
# 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?

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

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 $:.

···

--
Nobu Nakada

Logan Capaldo wrote:

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

Unfortuately that's not cross-platform.

T.

Robert Klemme wrote:

> 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.

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.

···

On 08.08.2006 18:06, Trans wrote:

nobu@ruby-lang.org wrote:

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.

···

--
Nobu Nakada

Trans wrote:

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

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" :slight_smile: ) 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.

···

On Aug 8, 2006, at 3:40 PM, Trans wrote:

Logan Capaldo wrote:

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

Unfortuately that's not cross-platform.

T.