[Rubygems-developers] __FILE__ hacks -was: Re: [...] Adoption

Hugh Sasse Staff Elec Eng hgs at dmu.ac.uk
Fri Oct 22 05:23:50 EDT 2004

On Thu, 21 Oct 2004 chad at chadfowler.com wrote:

>> That said, if RubyGems normalized the directory structures, numbering
>> schemes, etc of the upstream sources, it *could* become the input format
>> of choice for repackagers...
> This is actually what I had in mind.  Especially finding places where
> people rely on, for example, __FILE__ hacks that make it hard for
> everyone.  I'm optimistic that a focused effort with enough feedback to

My brain produced a pop-up when I read this :-). In the new PickAxe
you talk about testing on page 225, which refers people to the
techniques of Chapter 12 (pp 151ff) [though you didn't write Ch 12,
of course].  On page 160 is just such a
__FILE__ hack to get around paths being odd after installation.  Is
there a way to avoid this?  Can I put a method of Gem in place of
this, or something?  For those without the book to hand, the code I
mean is

    $:.unshift File.join(File.dirname(__FILE__), ",,", "lib")

I noticed that the directory structures in both chapters agreed
(p159, 232).

> the original developers could result in a behavioral shift in at least a
> decent number of them.  My guess is that in most cases, developers that
> release packages with this kind of problem do so because they didn't
> really consider them to be a problem (as opposed to having done these
> things intentionally).

I've used the 'if __FILE__ == $0' hack lots of times to ensure that
"libraries" (rather grand term for some of them!) have the test code
with them where it won't get lost, and if you invoke it as a command
by mistake it will do something sensible (test itself).  Maybe
there's an elegant way to do the same thing with test code in
another directory....

While musing about this: Should rubygems have the capability to
generate "boilerplate" for a new project: create the directories
project, project/lib, project/doc, project/tests, and maybe write
stubby versions of project/lib/project.rb,
project/tests/ts_project.rb, etc?  For all the "Ubiquitous
Automation" reasons.  It could then encourage people to do this sort
of thing correctly.

> Chad


More information about the Rubygems-developers mailing list