require namespaces

Discussion in 'Ruby' started by Trans, Sep 26, 2005.

  1. Trans

    Trans Guest

    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.
    Trans, Sep 26, 2005
    #1
    1. Advertising

  2. Trans

    Trans Guest

    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.
    Trans, Sep 29, 2005
    #2
    1. Advertising

  3. Trans

    Trans Guest

    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.
    Trans, Sep 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valentin tihomirov

    namespaces

    valentin tihomirov, May 24, 2004, in forum: VHDL
    Replies:
    0
    Views:
    593
    valentin tihomirov
    May 24, 2004
  2. Craig Deelsnyder

    Best Practices - solution - namespaces - classes

    Craig Deelsnyder, Aug 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    430
    Vincent V
    Aug 4, 2003
  3. Will

    Conflicting namespaces??

    Will, Aug 13, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,687
    Chris R. Timmons
    Aug 13, 2003
  4. Robert Billing

    Namespaces and require

    Robert Billing, Mar 26, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    90
    Robert Billing
    Mar 27, 2009
  5. Replies:
    56
    Views:
    711
    alex23
    Dec 27, 2012
Loading...

Share This Page