# Most used pwd in ruby

**URL:** <https://www.ruby-forum.com/t/most-used-pwd-in-ruby/154446>\
**Category:** Ruby\
**Created:** [November 29, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/most-used-pwd-in-ruby/154446 "2008-11-29T19:06:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [November 29, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/most-used-pwd-in-ruby/154446/1 "2008-11-29T19:06:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [November 29, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/most-used-pwd-in-ruby/154446/2 "2008-11-29T19:56:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [November 29, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/most-used-pwd-in-ruby/154446/3 "2008-11-29T21:15:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [November 29, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/most-used-pwd-in-ruby/154446/4 "2008-11-29T22:07:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saknarede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saknarede](https://www.ruby-forum.com/u/saknarede)\
**Post date:** [November 29, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/most-used-pwd-in-ruby/154446/5 "2008-11-29T23:10:48Z")

</div>

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!
