How to find directory of currently running .rb file

FILE is just giving me the file name, with no path, and Dir.getwd
is giving me an error: “ENOENT: No such file or directory.” What is
the proper way to get this info?

Thanks in advance,
Ken McDonald

On Mon Oct 27 19:01:04 2008, Kenneth McDonald wrote:

FILE is just giving me the file name, with no path, and Dir.getwd is
giving me an error: “ENOENT: No such file or directory.” What is the proper
way to get this info?

FILE gives you the path it was called with, if your Ruby script is
in the current working directory it will return “./foo.rb” if it is in
/usr/local/bin FILE will be “/usr/local/bin/foo.rb”. If you want
the full path instead of “./” Dir.getwd should work.

Kenneth McDonald wrote:

FILE is just giving me the file name, with no path, and Dir.getwd
is giving me an error: “ENOENT: No such file or directory.” What is
the proper way to get this info?

File.dirname(FILE)

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