Most used pwd in ruby

S

Sak Na rede

Hi all!

I have to program a lot of scripts, most of them work with files and
folders. This files and the folders are in the directory of the script,
it works well except if script is called from another directory. I solve
this problem with:

Dir.chdir("complete_path")

I think this solution is not the best for a lot of reasons, how do you
do that?

Thanks!!
 
R

Robert Klemme

File.dirname(__FILE__) always refers to the current folder.

Or rather File.dirname($0) if you want the main script file's directory.
__FILE__ depends on the currently executed file which can be anywhere.

See also "ri Pathname" for other options.

Kind regards

robert
 
M

Marc Heiler

Dir.chdir("complete_path")

It works, but I think it is not a perfect approach. What is better is
to, IMHO, work with absolute paths as much as possible. Ruby on windows
seems to happily accept "/foo/bla" locations so there really is not work
involved for my scripts when I use them on my windows machine (which is
mostly turned off these days)

Aside from that I guess most people here will use relative paths.
 
S

Sak Na rede

Marc said:
It works, but I think it is not a perfect approach. What is better is
to, IMHO, work with absolute paths as much as possible. Ruby on windows
seems to happily accept "/foo/bla" locations so there really is not work
involved for my scripts when I use them on my windows machine (which is
mostly turned off these days)

Aside from that I guess most people here will use relative paths.

I'm working in unix system and my scripts will always run in unix but
it's a very important know that. Thanks

Thanks Matthias and robert, very simple and very helpful, perfect!
 

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

No members online now.

Forum statistics

Threads
474,186
Messages
2,570,998
Members
47,587
Latest member
JohnetteTa

Latest Threads

Top