From: Robert Klemme Date: 2004-04-30T22:44:00+09:00 Subject: Re: File.expand_path(__FILE__) "Simon Strandgaard" schrieb im Newsbeitrag news:20040430134446.7d4d3fc1.neoneye@adslhome.dk... > "Robert Klemme" wrote: > > "Simon Strandgaard" schrieb im Newsbeitrag > > news:20040430124420.2f2b621b.neoneye@adslhome.dk... > > > "Robert Klemme" wrote: > > > > "Simon Strandgaard" schrieb im Newsbeitrag > > > > > > > > > > Maybe extend __FILE__ with a #absolute method, > > > > > in order to make __FILE__ immune against chdir ? > > > > > > > > This is easily fixed: > > > > > > > > FILE = File.expand_path __FILE__ > > > > p [__FILE__, FILE] > > > > > > Not if the person has done a chdir just before expand_path. > > > > Well, I should've written "first line in script". > > ok ;-) > > > > > 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 > > > > Slightly against, because > > > > - it's easily fixed > > Agree.. however see below > > > > - you loose information: there might be situations where the script wants > > to know the exact path it was invoked with. > > I think its the wrong place to retrive this kind of information. > Broken metaphor... I have used it myself, but without thinking about it. Where would you expect it to reside? Maybe the best solution is $0 : path as used for invocation __FILE__: abs path to current file Then I agree. :-) Any objections? Does this break existing code? > > - most of the time (when invoked from the shell via path expansion) > > scripts are invoked with an absolute path anyway. > > Please elaborate.. I don't understand. > > :-) 15:35:31 [ContentReporter_BRANCH]: PATH="${PATH}:/c/temp/ruby" bash -c 'echo $PATH; dollar0.rb' :/c/temp/ruby __FILE=/c/temp/ruby/dollar0.rb $0=/c/temp/ruby/dollar0.rb 15:35:51 [ContentReporter_BRANCH]: cat /c/temp/ruby/dollar0.rb #!/usr/bin/ruby puts "__FILE=#{__FILE__}" puts "$0=#{$0}" The invocation (-c 'dollar0.rb') does not use a path but rather the shell finds the executable based on its $PATH. Regards robert