Forum: Ruby How to find directory of currently running .rb file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
47df9cfb356c3ee0523cc3571b169730?d=identicon&s=25 Kenneth McDonald (Guest)
on 2008-10-27 20:02
(Received via mailing list)
__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
48ca8615b653b60a9079312fc832da44?d=identicon&s=25 Fred Phillips (Guest)
on 2008-10-27 20:21
(Received via mailing list)
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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-27 22:18
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 topic is locked and can not be replied to.