Newbie question - how to get current directory into a variab

P

Peter Vanderhaden

I'm running Ruby on Ubuntu Linux, and I need to be able to get the
current directory that the program is running in into a variable in my
program. Is there a simple way to do this? Also, is there a way to
change directories within the program?
Thanks....
 
T

Tim Hunter

Peter said:
I'm running Ruby on Ubuntu Linux, and I need to be able to get the
current directory that the program is running in into a variable in my
program. Is there a simple way to do this? Also, is there a way to
change directories within the program?
Thanks....

Check out the Dir class, particularly Dir.pwd and Dir.chdir,
 
P

Peter Vanderhaden

Thanks Tim. the Dir.pwd is exactly what I need. I haven't figured out
how to use the Dir.chdir yet, but that looks promising too.
PV
 
7

7stud --

Peter said:
Thanks Tim. the Dir.pwd is exactly what I need. I haven't figured out
how to use the Dir.chdir yet, but that looks promising too.

p Dir.entries(".") #array containing files in current directory
puts

Dir.chdir("../") #relative path
p Dir.entries(".")
puts

Dir.chdir("/Users/me/Documents/ruby") #absolute path
p Dir.entries(".")
 
J

Janus Bor

Sorry for raising this thread from the dead!
p Dir.entries(".") #array containing files in current directory
puts

Will this work on Windows, too? Or is the "." a Linux specific thing? If
it doesn't work on Windows, how can I get the absolute path of the
scipt? Thanks!
 
D

Dave Bass

Janus said:
Will this work on Windows, too? Or is the "." a Linux specific thing?

Works for me (WinXP, Ruby 1.8.6-111).
Both "." and ".." work fine on Windows.

Dave
 
S

Sean Willis

Thought I'd keep this one going!

Using Dir.chdir() I have no problem changing to directories created by
my rails application, however, I can't get it to change to a directory
that I made. I tried adding "config.load_paths += %W(
#{RAILS_ROOT}/public/my_new_folder" to my environment.rb file, but it
didn't work. When should I be using config.load_paths?

Any ideas? Thanks!
 

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
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top