From: John Platte Date: 2004-04-30T23:13:11+09:00 Subject: Re: File.expand_path(__FILE__) The OP yawns and wakes up to a big thread that has sprouted up where he posted his little codeling... On 2004 Apr 30, at 6:39, Robert Klemme wrote: >> I wonder why __FILE__ isn't an absolute path. Relative paths is >> just too fragile. >> >> >> Proposal: make __FILE__ an absolute path. >> That would make __FILE__ robust to Dir.chdir. >> >> >> Any thoughts on this proposal? fore or against That was my expectation. I really don't understand why __FILE__ would be relative. I did a lot of work in PHP before coming to Ruby, and PHP uses an absolute path in its __FILE__, which is a wonderful thing when trying to create programs that are robust against odd web server configurations. I see Perl uses relative paths for both __FILE__ and $0...and since a lot of Ruby scripts use "if __FILE__ == $0", those two should keep following the same policy. So I don't expect that behavior to change for my odd little case. Might there be another place to put this absolute path? (Or might one exist already?) PHP defines a global $SCRIPT_FILENAME... > Slightly against, because > > - it's easily fixed but obviously from the head-scratching on this thread, the current behavior at least threatens POLS. > - you loose information: there might be situations where the script > wants > to know the exact path it was invoked with. That seems like more of a special case to me than knowing exactly where we live in the filesystem, but I'll admit that if Ruby got this far with this behavior, maybe I'm not in the majority. > - most of the time (when invoked from the shell via path expansion) > scripts are invoked with an absolute path anyway. My situation: I was invoking a utility script from Vim's shell, and it had a bug that dumped output in the wrong directory. As I hacked a fix, somehow I wound up with code that triggered the unexpected behavior I posted about (I apologize for not pointing out which part was unexpected). Whatever the solution, I want it to be easy to get an authoritative absolute path for the current file from Ruby; I'd strongly prefer that it not be fragile to chdirs, since that's a side effect that reduces flexibility. Surely there's an existing solution to this within Ruby or Perl or some other language? Thanks to the other posters for the workaround(s) and explanation. -- Ryan "John" Platte Custom services, NIKA Consulting http://nikaconsulting.com/