Determining the directory a script is running in

K

Kev Jackson

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
 
K

Kev Jackson

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
 
S

Sam Gentle

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
 
S

Stefan Lang

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?

Dir.pwd is what you are looking for.

$ cd /home/stefan/tmp
$ irb
irb(main):001:0> Dir.pwd
=> "/home/stefan/tmp"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top