[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[ANN] Better relative requires with Need

Drew Olson

2/9/2008 10:55:00 PM

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

16 Answers

ara.t.howard

2/9/2008 11:29:00 PM

0


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://draw...
--
sleep is the best meditation.
h.h. the 14th dalai lama




Kevin Barnes

2/10/2008 2:42:00 PM

0

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

Drew Olson

2/10/2008 4:35:00 PM

0

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

Bob Hutchison

2/11/2008 1:58:00 PM

0

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.recurs...
Recursive Design Inc. -- weblog at http://www.recursiv...
http://www.rec... -- works on http://www.raconteur.info/cms-for-static-con...



ara.t.howard

2/11/2008 4:26:00 PM

0


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://draw...
--
sleep is the best meditation.
h.h. the 14th dalai lama




Rob Sanheim

2/11/2008 6:04:00 PM

0

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://robs...
http://thinkrel...


On Feb 11, 2008 11:25 AM, ara howard <ara.t.howard@gmail.com> 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://draw...
> --
> sleep is the best meditation.
> h.h. the 14th dalai lama
>
>
>
>
>

Drew Olson

2/11/2008 6:29:00 PM

0

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

The Peeler

6/7/2013 7:46:00 PM

0

On Fri, 07 Jun 2013 12:42:07 -0700, The Rectum, the resident psychopath of
sci and scj, FAKING his time zone again and IMPERSONATING his master, The
Peeler, wrote:


>>
>>i have no foreskin i got circumsized the ,
>>i got the turtle neck shaved off ,
>>my dick is just a dick head
>>
>>http://www.youtube.com/watch?v=m...
>
> Nothing, for me, to peel then innit! <G>

Not only that, not even anything to finger for you:

Picture of The Rectum with nothing to finger:

http://img534.imageshack.us/img534/5800/neute...

--
P-Dub about The Rectum:
"The REVD doesn't have to be neutered. He already has no balls. No job.
No skills. No friends. No woman AND no balls. No wonder he's such a
'pleasant' fellow."
MID: <RWvet.2816$5U.1142@newsfe16.iad>

The Peeler

6/7/2013 7:48:00 PM

0

On Fri, 07 Jun 2013 12:44:48 -0700, The Rectum, the resident psychopath of
sci and scj, FAKING his time zone again and IMPERSONATING his master, The
Peeler, wrote:

>>>
>>> No you, just did.
>>
>>i knew you would admit you did
>
> I knew you would innit!

You know from nothing, The Rectum! You ARE a retard!

--
Retarded, anal, subnormal and extremely proud of it: our resident
psychopath, The Retard (aka "The Rectum").

%

6/7/2013 7:52:00 PM

0

The Peeler wrote:
> On Fri, 7 Jun 2013 12:34:02 -0700, "%" <persent@gmail.com> wrote:
>
>> The Peeler wrote:
>>> On Fri, 7 Jun 2013 12:19:13 -0700, "%" <persent@gmail.com> wrote:
>>>
>>>> The Peeler wrote:
>>>>> On Fri, 7 Jun 2013 11:35:10 -0700, "%" <persent@gmail.com> wrote:
>>>>>
>>>>>> The Peeler wrote:
>>>>>>> On Fri, 7 Jun 2013 19:55:46 +0200, The Peeler
>>>>>>> <finishingoff@themoronicRevd.invalid> wrote:
>>>>>>>
>>>>>>>> On Fri, 07 Jun 2013 10:48:08 -0700, The Rectum, the resident
>>>>>>>> psychopath of sci and scj, FAKING his time zone again and
>>>>>>>> IMPERSONATING his master, The Peeler, wrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> The, best place for us Brian innit! <GB>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> PEEEEEEEEEEEEEEEEEEEEEEE LIAR
>>>>>>>>>
>>>>>>>>> PUSSSSSSSSSSSSSSSENT!!! <G>
>>>>>>>>
>>>>>>>> I'M AN ASSSSSSSSSSSSSSSSHOLE!!! <BG>
>>>>>>>
>>>>>>> YOU, ARE! <GB>
>>>>>>
>>>>>> no you do
>>>>>
>>>>> No I don't innit!
>>>>
>>>> you just did
>>>
>>> No you, just did.
>>
>> i knew you would admit you did
>
> I knew you would innit!

yes you did do that