require namespaces

T

Trans

mypath/file1.rb:

require 'file2.rb'
require 'file3.rb'

mypath/file2.rb:

# ...

anotherpath/file3.rb:

# ...

Then do
$LOAD_SPACE['myspace'] = [ 'mypath', 'anotherpath' ]
require 'myspace:file1.rb'
=> true

How it works is to put the namespace paths on the $LOAD_PATH when a
namespace is encountered in a require string, then removes them when
done.

Thoughts?

T.
 
T

Trans

My require namspaces idea worked pretty well. It allows one to free the
organization of your libs from how the end-user will interface with
them. For example:

mylib/
foo/
scriptA.rb
bar/
scriptB.rb

Then

$LOAD_SPACE['myspace'] = [ 'mylib/foo', 'mylib/bar' ]
require 'myspace:scriptA.rb'
=> true

Then I refined it so this works too:

require 'myspace/scriptA.rb'

The other thing it does is that scriptA.rb and scriptB.rb (or any other
scipt within the same namespace) can require another withput specifying
the location, ie.

scriptA.rb:
require 'scriptB.rb'

No need for 'myspace/script2.rb'. This works as long as scriptA.rb is
required with a defined namespace.

So all was well and wonderful, but then a nastye problem crept up -- I
couldn't run one of these scripts on its own (like for testing
purposes) b/c require's wouldn't be found. (i.e. the namespace isn't
setup) Ugh! Back to the drawing board....

Well it took me awhile, but arrived at a solution. Next post...

T.
 
T

Trans

Okay, so I wanted a complete solution. A better solution. So I came up
with the idea of adding a namespace file into my lib path that is
"aquisitioned" (zope term), that will put into place the namespaces.

mylib/
_ns.rb
foo/
scriptA.rb
bar/
scriptB.rb

In "_ns.rb" I have:

mylib/foo myspace
mylib/bar myspace

Yes, I know it's not really a ruby script, but setup.rb won't install
it unless it ends in .rb --also I may change it to an actual ruby
script in the future --then it will more like an "autorequire" file.
Anyway. This solves all problems. Except for one thing: it's very
tricky to code.

I've managed a semi-working implemenation, but it's lack luster and
fails under certain cases. So I wondering if anyone has any insight on
how this can be coded?

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top