Most used pwd in ruby


#1

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!!


#2

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


#3

On 29.11.2008 19:51, Matthias Hennemeyer wrote:

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


#4

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.


#5

Marc H. wrote:

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.

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!