On Sat, 1 May 2004, Simon Strandgaard wrote:

> "Ara.T.Howard" <ahoward / fattire.ngdc.noaa.gov> wrote:
> > > 
> > > it seems we can't determine the absolute path of __FILE__ correctly 
> > > after a chdir without having planned for that eventuality before the 
> > > chdir.
> > > 
> > > Can someone solve that one?
> > 
> > what's wrong with this, i do it in all my programs:
> > 
> > require 'pathname'
> > 
> > class Main
> >   ...
> >   FILE = Pathname.new(__FILE__).realpath.to_s
> >   ...
> > end
> > 
> > what's wrong with solving it by planning for it?
> 
> I don't intend to be non-friendly, but if you do so in all your Main
> classes, then it looks like kludge to me.  You wouldn't have to make such
> work-arounds if __FILE__ were with an absolute path.

i have to disagree - the reason i do it is that i drive alot of external
programs which dump their output in the cwd, therefore i end up doing a lot of
Dir.chdir's (see my dirstack package on RAA)...  i have several main templates
and a code generator which uses these templates to spit out a main program of
several common flavors.  one of things i find useful is knowing the absolute
path of the invoked program file because i'm __planning__ on doing a lot of
chdirs... some of my other templates do not include this.  by your logic my
option parsing would be a kludge since it always has a '--help' and '--log'
option and there should be a way to avoid such duplicate effort... then again
perhaps you are correct, witness perl's pod2usage.  code generators are a nice
way to get somewhere fast though...

> I don't know if all this rethinking __FILE__/$0 makes sense.. 

i think it makes perfect sense to have

  __FILE__    => absolute path of script file
  __PROGRAM__ => absolute path of called program
  __RUBY__    => absolute path of ruby interp running called program
  __DIR__     => absolute directory of file (can use for relative requires)

it's always easy get a relative path from an absolute one, but not the other
way!

cheers.

-a
-- 
===============================================================================
| EMAIL   :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE   :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL     :: http://www.ngdc.noaa.gov/stp/
| TRY     :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done 
===============================================================================