Ruby Scripts Location

Hi All

I know this should be very simple but I can’t for the life of me find
the answer using Pickaxe or Google.

I want to be able to access the location/path of the currently
executing Ruby script. I thought it was a global variable “$” but
looking at the lists I’ve seen I can’t find it.

Your help would be much appreciated.

Thanks.

Doug

Doug B. wrote:

I want to be able to access the location/path of the currently
executing Ruby script. I thought it was a global variable “$” but
looking at the lists I’ve seen I can’t find it.

$0 is probably what you want. Use File.expand_path on it to get the full
path.

$0, or FILE in the main script file.

Gennady.

On Jul 10, 2006, at 10:57 AM, Joel VanderWerf wrote:

$0 is probably what you want. Use File.expand_path on it to get the
full path.

A couple of points to mention. 1) You can use $PROGRAM_NAME instead
of $0, it’s
a little more readable. 2) Make sure you have not done a Dir.chdir
before the File.expand_path

% cat path.rb
puts $PROGRAM_NAME
puts File.expand_path($PROGRAM_NAME)
Dir.chdir “pdf”
puts File.expand_path($PROGRAM_NAME)

% ruby path.rb
path.rb
/Users/jdf/projects/devel/ruby_test_code/path.rb
/Users/jdf/projects/devel/ruby_test_code/pdf/path.rb


vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Jim F.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs