[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

load paths unions?

Trans

8/8/2006 4:07:00 PM

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.

9 Answers

Robert Klemme

8/8/2006 4:20:00 PM

0

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.

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

Logan Capaldo

8/8/2006 4:40:00 PM

0


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

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



Trans

8/8/2006 7:40:00 PM

0


Logan Capaldo wrote:

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

Unfortuately that's not cross-platform.

T.

Trans

8/8/2006 7:46:00 PM

0


Robert Klemme wrote:
> 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.
>
> 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.


Robert Klemme

8/8/2006 8:56:00 PM

0

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

Logan Capaldo

8/8/2006 9:33:00 PM

0


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

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.


Peña, Botp

8/9/2006 1:20:00 AM

0

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 :)

Nobuyoshi Nakada

8/9/2006 7:41:00 AM

0

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

Trans

8/11/2006 8:22:00 PM

0


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