Forum: Ruby Most used pwd in ruby

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.
D208930fb4a8fb858658a717081da813?d=identicon&s=25 Sak .. (saknarede)
on 2008-11-29 19:06
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!!
4afe6410564deb5174ebf01206df4ea3?d=identicon&s=25 Matthias Hennemeyer (mhennemeyer)
on 2008-11-29 19:56
File.dirname(__FILE__) always refers to the current folder.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-11-29 21:15
(Received via mailing list)
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
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2008-11-29 22:07
> 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.
D208930fb4a8fb858658a717081da813?d=identicon&s=25 Sak .. (saknarede)
on 2008-11-29 23:10
Marc Heiler 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!
This topic is locked and can not be replied to.