From: Matthew Hailstone Date: 2007-01-12T06:03:53+09:00 Subject: Re: Finding path to ruby script argument helloworld.rb: ------------------------------ puts $0 puts __FILE__ puts File.dirname($0) puts ARGV[0] puts File.expand_path(__FILE__) puts File.expand_path($0) require 'includeme.rb' tempvar = IncludeMe.new tempvar.runme ------------------------------ includeme.rb: ------------------------------ class IncludeMe def runme puts "includeme.rb" puts __FILE__ puts $0 puts "end includeme.rb" end end ------------------------------ command: ------------------------------ ruby helloworld.rb onlyarg ------------------------------ output: ------------------------------ helloworld.rb helloworld.rb . onlyarg C:/scripts/helloworld.rb C:/scripts/helloworld.rb includeme.rb ./includeme.rb helloworld.rb end includeme.rb ------------------------------ Looks like out of the originally invoked rb script, the whole path is lost. I would have to assign the value to a global variable if I needed to preserve it to all scripts. Matthew On 1/11/07, Philip Hallstrom wrote: > > On 1/11/07, Matthew Hailstone wrote: > >> Looks like __FILE__ and $0 are the same. > > > > Not necessarily... when running a script under rcov or similar, they > > may differ (for example one starts with ./ while the other does not). > > That's why I write the if __FILE__ == $0 idiom as > > > > if File.expand_path(__FILE__) == File.expand_path($0) > > > > Another possibility might be is when you start the script using $PATH, > > i.e. not from current directory, but without specifying its path. > > Also, and i haven't even bothered to test this, but if it's similar to > PHP's FILE variable, it's the *current* file... so require a file, call a > method in that file, and access __FILE__ in that method and you're gonna > get your required file, not the initial one... > > -philip > >