Forum: Ruby Which Directory was my ruby script run from?

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.
2b0aa9ebcfed37f564ebc00056466d6d?d=identicon&s=25 Matthew John (Guest)
on 2006-01-20 15:29
Hi All,

How do I find out which directory my ruby script was run from?

Thanks Matthew John
p.s Today I'm using Windows
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-20 15:37
(Received via mailing list)
On Jan 20, 2006, at 7:52 AM, Matthew John wrote:

> Hi All,
>
> How do I find out which directory my ruby script was run from?
>
> Thanks Matthew John
> p.s Today I'm using Windows

Try:

File.dirname($PROGRAM_NAME)

James Edward Gray II
357558a6682f4d6624594763d9acdb35?d=identicon&s=25 Mike Fletcher (fletch)
on 2006-01-20 15:46
Matthew John wrote:
> Hi All,
>
> How do I find out which directory my ruby script was run from?

Maybe Dir.getwd?

------------------------------------------------------------- Dir::getwd
     Dir.getwd => string
     Dir.pwd => string
------------------------------------------------------------------------
     Returns the path to the current working directory of this process
     as a string.

        Dir.chdir("/tmp")   #=> 0
        Dir.getwd           #=> "/tmp"
E5c79d5a6d378dc4a26e20435be31657?d=identicon&s=25 Jamey Cribbs (Guest)
on 2006-01-20 17:07
(Received via mailing list)
Mike Fletcher wrote:

>
>
I use File.dirname($0).

Jamey Cribbs

Confidentiality Notice: This email message, including any attachments,
is for the sole use of the intended recipient(s) and may contain
confidential and/or privileged information. If you are not the intended
recipient(s), you are hereby notified that any dissemination,
unauthorized review, use, disclosure or distribution of this email and
any materials contained in any attachments is prohibited. If you receive
this message in error, or are not the intended recipient(s), please
immediately notify the sender by email and destroy all copies of the
original message, including attachments.
2b0aa9ebcfed37f564ebc00056466d6d?d=identicon&s=25 Matthew John (Guest)
on 2006-01-20 17:34
I've written a little script to test, and it looks like the second reply
by Mike is the only one that works.  The other two provide me with the
location of the file, not the directory where the file was run from.

My script is in c:\sparkdriver\bin and this is set in my path.

When I run the the testfile.rb script from the c:\Documents and Settings
folder I get the following results:-

C:\Documents and Settings>testfile.rb
Physical Location of File c:\sparkdriver\bin

c:/sparkdriver/bin     - File.dirname($PROGRAM_NAME)
C:/Documents and Settings     - Dir.getwd
c:/sparkdriver/bin     - File.dirname($0)

---- testfile.rb

puts "Physical Location of File c:\\sparkdriver\\bin\n\n"

dir = File.dirname($PROGRAM_NAME)
puts dir + '     - File.dirname($PROGRAM_NAME)'

dir = Dir.getwd
puts dir + '     - Dir.getwd'

dir =File.dirname($0)
puts dir + '     - File.dirname($0)'

Thanks for your help.

Matthew John
018f1aa74fcc154beaa6408951d10776?d=identicon&s=25 Tim Hammerquist (Guest)
on 2006-01-24 16:57
(Received via mailing list)
Matthew John <mj@mjcltd.net> wrote:
> I've written a little script to test, and it looks like the
> second reply by Mike is the only one that works.  The other
> two provide me with the location of the file, not the
> directory where the file was run from.

Excellent!  I love seeing posters get resourceful with the
answers they receive!  This makes me happy.  :)

Yes, File.dirname and Dir.getwd do two very different things,
which you discovered.  There seems to have been a small
misunderstanding.

File.dirname simply does string manipulation on its argument,
whereas Dir.getwd actually gets the directory from which the
current script ($0) was invoked... which is what you were asking
for.

BTW, $0 and $PROGRAM_NAME are aliases for the same variable.

> Thanks for your help.

Thank you for making the most of the help offered!

Cheers!
Tim Hammerquist
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 Gene Tani (Guest)
on 2006-01-24 17:00
(Received via mailing list)
Tim Hammerquist wrote:

> BTW, $0 and $PROGRAM_NAME are aliases for the same variable.
>

"__FILE__", too, I guess
357558a6682f4d6624594763d9acdb35?d=identicon&s=25 Mike Fletcher (fletch)
on 2006-01-24 17:36
Gene Tani wrote:
> Tim Hammerquist wrote:
>
>> BTW, $0 and $PROGRAM_NAME are aliases for the same variable.
>>
>
> "__FILE__", too, I guess

Nope.  __FILE__ is the name current source file, which can be different
than $0 in different modules.

freebie:~ 839> ruby aprog.rb
$0: aprog.rb
__FILE__: ./amodule.rb
aprog $0: aprog.rb
aprog __FILE__: aprog.rb
freebie:~ 840> cat aprog.rb
#!/usr/local/bin/ruby
require 'amodule'

f = Foo.new

f.pr_dollar0
f.pr_file

puts "aprog $0: #{$0}"
puts "aprog __FILE__: #{__FILE__}"

exit 0

__END__
freebie:~ 841> cat amodule.rb
class Foo
  def pr_file
    puts "__FILE__: #{__FILE__}"
  end
  def pr_dollar0
    puts "$0: #{$0}"
  end
end
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-01-24 19:24
(Received via mailing list)
"Gene Tani" <gene.tani@gmail.com> writes:

> Tim Hammerquist wrote:
>
>> BTW, $0 and $PROGRAM_NAME are aliases for the same variable.
>>
>
> "__FILE__", too, I guess

No, __FILE__ is the current file, while $0 is the script ruby was
started with.
This topic is locked and can not be replied to.