# Paths, gentleman, paths

**URL:** <https://www.ruby-forum.com/t/paths-gentleman-paths/74415>\
**Category:** Ruby\
**Created:** [November 6, 2006, 2:13pm UTC](https://www.ruby-forum.com/t/paths-gentleman-paths/74415 "2006-11-06T14:13:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [November 6, 2006, 2:13pm UTC](https://www.ruby-forum.com/t/paths-gentleman-paths/74415/1 "2006-11-06T14:13:45Z")

</div>

I have this bit of code in the beginning of an application I’m writing  
(in main.rb):

require ‘pathname’

p = Pathname.new($0)

if p.basename.to\_s == ‘main.rb’  
Dir.chdir p.parent.to\_s  
end

require ‘gui/main\_window’  
… # (rest of requires, application itself)

The check whether it’s “main.rb” or not is so this doesn’t happen for  
Rake. Now, it works well, but it’s ugly. The reason I don’t just go  
ahead and use ‘:’ is that I use glade, as well as some other files which  
I need to be able to find. This is currently in development, so I’d  
prefer to avoid forcing people to install the program in predetermined  
locations (/usr/local/bin, /usr/local/share/my\_app\_files, et cetera).

Any cleaner solution?

---

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [November 7, 2006, 11:06am UTC](https://www.ruby-forum.com/t/paths-gentleman-paths/74415/2 "2006-11-07T11:06:22Z")

</div>

On 06.11.2006 14:13, Ohad L. wrote:

> Any cleaner solution?

Dir.chdir( File.dirname( $0 ) ) if File.basename $0 == ‘main.rb’

or

dir, file = File.split $0  
Dir.chdir dir if file == ‘main.rb’

Cheers

```
robert
```

---

<div class="post-metadata">

**Author:** ![ohad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f04885/32.png) [@ohad](https://www.ruby-forum.com/u/ohad)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/paths-gentleman-paths/74415/3 "2006-11-07T11:08:43Z")

</div>

Ohad L. wrote:

> The check whether it’s “main.rb” or not is so this doesn’t happen for  
> Rake. Now, it works well, but it’s ugly. The reason I don’t just go  
> ahead and use ‘:’ is that I use glade, as well as some other files which  
> I need to be able to find. This is currently in development, so I’d  
> prefer to avoid forcing people to install the program in predetermined  
> locations (/usr/local/bin, /usr/local/share/my\_app\_files, et cetera).

Have your startup script/s determine the “root” of your application’s  
directory structure, and use a global to refer to all other resources  
using absolute paths from this root path? (Yes, I know globals are evil,  
but the current working directory is global state too, so it’s at least  
not a worse solution.)

Depending on being chdirred into your directory structure or on  
predetermined install locations is Bad, smells too much of “I know  
what’s good for my users better than them” to me.

David V.
