Forum: Ruby Determining the directory a script is running in

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.
kevin.jackson (Guest)
on 2005-11-17 08:49
(Received via mailing list)
How can I find the directory that the current script is running from?

I need to run a script and (without any arguments), assign a variable
based on the directory that the script is being executed from, ie

c:\my_dir> ruby script.rb

-> @dir = c:\my_dir

or in unix

/home/kj/ruby-projects/ruby script.rb

-> @dir = /home/kj/ruby-projects/

I can't find what I'm looking for in the pickaxe, can anyone help out?

Kev
kevin.jackson (Guest)
on 2005-11-17 08:58
(Received via mailing list)
Sorry, I discovered that File.expand_path(".") does work after all -
strange I thought I'd tried it and got an error, perhaps it was
something else

Kev
sgentle (Guest)
on 2005-11-17 10:23
(Received via mailing list)
On 11/17/05, Kev J. <removed_email_address@domain.invalid> wrote:
> Sorry, I discovered that File.expand_path(".") does work after all -
> strange I thought I'd tried it and got an error, perhaps it was
> something else

You may also find Dir.getwd/Dir.pwd useful.

Sam
langstefan (Guest)
on 2005-11-17 10:32
(Received via mailing list)
On Thursday 17 November 2005 07:48, Kev J. wrote:
>
> or in unix
>
> /home/kj/ruby-projects/ruby script.rb
>
> -> @dir = /home/kj/ruby-projects/
>
> I can't find what I'm looking for in the pickaxe, can anyone help
> out?

Dir.pwd is what you are looking for.

  $ cd /home/stefan/tmp
  $ irb
  irb(main):001:0> Dir.pwd
  => "/home/stefan/tmp"
This topic is locked and can not be replied to.