From: Robert Klemme Date: 2004-04-30T23:39:01+09:00 Subject: Re: File.expand_path(__FILE__) "John Platte" schrieb im Newsbeitrag news:81CD2517-9AB0-11D8-82AE-000A95EB0812@nikaconsulting.com... > 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. .... or operators are overloaded to ensure proper behavior. > 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. Both may be true. :-) > > - 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? See my other post: # at the top of script FILE = File.expand_path( __FILE __ ) > Thanks to the other posters for the workaround(s) and explanation. We love to bang our heads on simple looking but difficult to solve problems. :-) robert