[ANN] Better relative requires with Need

Discussion in 'Ruby' started by Drew Olson, Feb 9, 2008.

  1. Drew Olson

    Drew Olson Guest

    Need makes ruby relative requires just work. Simply need a file with a
    relative path
    and the file will always be required correctly, regardless of what file
    your application is
    being launched through. Typically, ruby projects would unshift lib onto
    $PATH or use the
    File.dirname(__FILE__) trick. Using need means you don't have to worry
    about either of these.

    Assume you have two files, one directly in lib and the other in
    lib/extensions. Let's assume that file_a in lib requires file_b, in
    lib/extensions. Previously, you would doing some crazy load path
    unshifting or use the __FILE__ trick to make these requires flexible
    enough to work when your app is being accessed by rake, through a test
    suite, or required as a gem. Now, just use need.

    In file_a:
    need{"extensions/file_b"}

    Note that the block syntax is necessary. Need uses the binding of the
    block to determine the
    location of your file and correct perform your relative require for you.

    == INSTALL:

    * sudo gem install need

    Hope you enjoy it!

    - Drew
    --
    Posted via http://www.ruby-forum.com/.
    Drew Olson, Feb 9, 2008
    #1
    1. Advertising

  2. Drew Olson

    ara howard Guest

    On Feb 9, 2008, at 3:54 PM, Drew Olson wrote:

    > Note that the block syntax is necessary. Need uses the binding of the
    > block to determine the
    > location of your file and correct perform your relative require for
    > you.


    nice idea!

    a @ http://drawohara.com/
    --
    sleep is the best meditation.
    h.h. the 14th dalai lama
    ara howard, Feb 9, 2008
    #2
    1. Advertising

  3. Drew Olson

    vinbarnes Guest

    Re: Better relative requires with Need

    Kudos! I had worked on something similar although I didn't use the
    block binding which is a better way to go. I alias_method_chained
    require and was using the caller stack to figure out the correct file
    path. But I kept running into the problem of having to require it
    everywhere... Like I would be in a test file and you'd have to have,

    require 'elreq' # name of mine
    require '../../test_helper'

    So I largely gave up b/c while it was cleaner, adding the extra
    require everywhere seemed to negate the benefits. If only it was
    globally available - like in core. :)

    A suggestion, if you are concerned with portability you may want to
    use File.join to concatenate the end result together. Also, you
    probably want to stick it in the Kernel module, but that may just be a
    design pet peeve of mine.

    Glad something like this is out there.

    Cheers,
    Kevin
    vinbarnes, Feb 10, 2008
    #3
  4. Drew Olson

    Drew Olson Guest

    Re: Better relative requires with Need

    Kevin Barnes wrote:
    > Glad something like this is out there.
    >
    > Cheers,
    > Kevin


    Kevin -

    Thanks so much for the response. I'm glad people may find it useful. As
    far as joining the file path, what exactly does this buy me? I've seen
    it used quite a bit, but never used it myself. Also, in regard to
    extending Kernel rather than Object, isn't Kernel itself an Object? Or
    is it seen as too intrusive to extend object?

    Thanks for the advice and please feel free to submit a patch, I'll
    definitely take a look at adding it to the source.

    Thanks,
    Drew
    --
    Posted via http://www.ruby-forum.com/.
    Drew Olson, Feb 10, 2008
    #4
  5. Re: Better relative requires with Need

    Hi,

    On 10-Feb-08, at 11:34 AM, Drew Olson wrote:

    > As
    > far as joining the file path, what exactly does this buy me?


    You can avoid the problem (leaving it entirely in the hands of your
    users) if you write it this way:

    File.expand_path(block.call,
    File.dirname(eval("__FILE__",block.binding)))

    And it's shorter.

    Anyway, thanks for the gem!

    Cheers,
    Bob

    ----
    Bob Hutchison -- tumblelog at http://www.recursive.ca/so/
    Recursive Design Inc. -- weblog at http://www.recursive.ca/hutch
    http://www.recursive.ca/ -- works on http://www.raconteur.info/cms-for-static-content/home/
    Bob Hutchison, Feb 11, 2008
    #5
  6. Drew Olson

    ara howard Guest

    Re: Better relative requires with Need

    On Feb 10, 2008, at 9:34 AM, Drew Olson wrote:

    > Thanks so much for the response. I'm glad people may find it u
    > seful. As
    > far as joining the file path, what exactly does this buy me?



    i always expand and join the path because otherwise doing

    require '../a.rb'

    followed by

    require '.././a.rb'

    will cause ruby to load the file twice - the hash is on the *exact*
    filename

    cheers.

    a @ http://drawohara.com/
    --
    sleep is the best meditation.
    h.h. the 14th dalai lama
    ara howard, Feb 11, 2008
    #6
  7. Drew Olson

    Rob Sanheim Guest

    Re: Better relative requires with Need

    I believe this should be handled by ruby's require in 1.9...so no more
    double requires based on differing file paths.

    - Rob

    http://robsanheim.com
    http://thinkrelevance.com


    On Feb 11, 2008 11:25 AM, ara howard <> wrote:
    >
    > On Feb 10, 2008, at 9:34 AM, Drew Olson wrote:
    >
    > > Thanks so much for the response. I'm glad people may find it u
    > > seful. As
    > > far as joining the file path, what exactly does this buy me?

    >
    >
    > i always expand and join the path because otherwise doing
    >
    > require '../a.rb'
    >
    > followed by
    >
    > require '.././a.rb'
    >
    > will cause ruby to load the file twice - the hash is on the *exact*
    > filename
    >
    > cheers.
    >
    >
    > a @ http://drawohara.com/
    > --
    > sleep is the best meditation.
    > h.h. the 14th dalai lama
    >
    >
    >
    >
    >
    Rob Sanheim, Feb 11, 2008
    #7
  8. Drew Olson

    Drew Olson Guest

    Re: Better relative requires with Need

    Kevin Barnes wrote:
    > A suggestion, if you are concerned with portability you may want to
    > use File.join to concatenate the end result together.


    Kevin -

    I read up on this and it makes quite a bit of sense. I'll patch the code
    and release a new version.

    Thanks!

    - Drew
    --
    Posted via http://www.ruby-forum.com/.
    Drew Olson, Feb 11, 2008
    #8
    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. Zachary Hilbun
    Replies:
    4
    Views:
    550
    Zachary Hilbun
    Dec 20, 2004
  2. Amelyan
    Replies:
    3
    Views:
    611
    Amelyan
    Jun 17, 2005
  3. Peter Bencsik
    Replies:
    2
    Views:
    824
  4. Intransition
    Replies:
    19
    Views:
    178
    Aldric Giacomoni
    Nov 21, 2009
  5. Sherman Willden
    Replies:
    1
    Views:
    133
    Sisyphus
    Jul 25, 2003
Loading...

Share This Page