# File location

**URL:** <https://rubytalk.org/t/file-location/15719>\
**Category:** ruby-talk\
**Created:** [11 January 2005 03:10 UTC](https://rubytalk.org/t/file-location/15719 "2005-01-11T03:10:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eko\_Budi\_Setiyo](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@Eko\_Budi\_Setiyo](https://rubytalk.org/u/Eko_Budi_Setiyo)\
**Post date:** [11 January 2005 03:10 UTC](https://rubytalk.org/t/file-location/15719/1 "2005-01-11T03:10:19Z")

</div>

Hi everybody

#file1.rb  
#Location: /home/eko  
puts Dir.pwd require 'directory1/file2.rb

#file2.rb  
#Location: /home/eko/directory1  
puts Dir.pwd

if I run "ruby file1.rb  
will produce:  
"/home/eko"

What comand that I should you in file2.rb that will "automatically" produce result  
"/home/eko"  
"/home/eko/directory1/file2.rb"

regards  
Eko

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [11 January 2005 04:05 UTC](https://rubytalk.org/t/file-location/15719/2 "2005-01-11T04:05:31Z")

</div>

Eko Budi Setiyo wrote:

> Hi everybody
> 
> #file1.rb  
> #Location: /home/eko  
> puts Dir.pwd require 'directory1/file2.rb
> 
> #file2.rb  
> #Location: /home/eko/directory1  
> puts Dir.pwd
> 
> if I run "ruby file1.rb  
> will produce:  
> "/home/eko"
> 
> What comand that I should you in file2.rb that will "automatically" produce result  
> "/home/eko"  
> "/home/eko/directory1/file2.rb"

#file1.rb  
puts Dir.pwd # /tmp  
require 'directory1/file2.rb'

#file2.rb  
puts Dir.pwd # /tmp

Dir.chdir(File.dirname(\_\_FILE\_\_)) do  
&nbsp;&nbsp;&nbsp;puts Dir.pwd # /tmp/directory1  
end

puts Dir.pwd # /tmp

(But note that Dir.chdir with a block is not threadsafe.)

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [11 January 2005 07:32 UTC](https://rubytalk.org/t/file-location/15719/3 "2005-01-11T07:32:54Z")

</div>

Hi,

> **···**
>
> In message "Re: file location" on Tue, 11 Jan 2005 12:10:19 +0900, Eko Budi Setiyo \<contact\_us@haltebis.com\> writes:
> 
> > What comand that I should you in file2.rb that will "automatically"  
> > produce result  
> > "/home/eko"  
> > "/home/eko/directory1/file2.rb"
> 
> The current directory (which is returned from Dir.pwd) is something  
> different from the directory where the program are stored. What about  
> \_\_FILE\_\_ pseudo constant that shows the file name of the loading  
> program?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Eko\_Budi\_Setiyo](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@Eko\_Budi\_Setiyo](https://rubytalk.org/u/Eko_Budi_Setiyo)\
**Post date:** [11 January 2005 04:10 UTC](https://rubytalk.org/t/file-location/15719/4 "2005-01-11T04:10:39Z")

</div>

Thanks you very much

Joel VanderWerf wrote:

> **···**
>
> > Eko Budi Setiyo wrote:
> > 
> > > Hi everybody
> > > 
> > > #file1.rb  
> > > #Location: /home/eko  
> > > puts Dir.pwd require 'directory1/file2.rb
> > > 
> > > #file2.rb  
> > > #Location: /home/eko/directory1  
> > > puts Dir.pwd
> > > 
> > > if I run "ruby file1.rb  
> > > will produce:  
> > > "/home/eko"
> > > 
> > > What comand that I should you in file2.rb that will "automatically" produce result  
> > > "/home/eko"  
> > > "/home/eko/directory1/file2.rb"
> > 
> > #file1.rb  
> > puts Dir.pwd # /tmp  
> > require 'directory1/file2.rb'
> > 
> > #file2.rb  
> > puts Dir.pwd # /tmp
> > 
> > Dir.chdir(File.dirname(\_\_FILE\_\_)) do  
> > &nbsp;&nbsp;puts Dir.pwd # /tmp/directory1  
> > end
> > 
> > puts Dir.pwd # /tmp
> > 
> > (But note that Dir.chdir with a block is not threadsafe.)

---

<div class="post-metadata">

**Author:** ![Eko\_Budi\_Setiyo](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@Eko\_Budi\_Setiyo](https://rubytalk.org/u/Eko_Budi_Setiyo)\
**Post date:** [11 January 2005 09:23 UTC](https://rubytalk.org/t/file-location/15719/5 "2005-01-11T09:23:43Z")

</div>

Yukihiro Matsumoto wrote:

> Hi,
> 
> > What comand that I should you in file2.rb that will "automatically" produce result  
> > "/home/eko"  
> > "/home/eko/directory1/file2.rb"
> 
> The current directory (which is returned from Dir.pwd) is something  
> different from the directory where the program are stored. What about  
> \_\_FILE\_\_ pseudo constant that shows the file name of the loading  
> program?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

I litle bit feel shame to admit it, but your suggestion is the shortest solution.  
Why I can't find this \_\_FILE\_\_ in all the tutorial that i read before I submit to this mailling list  
\_\_FILE\_\_ is the exactly what I want

regards  
eko

> **···**
>
> > In message "Re: file location" \> on Tue, 11 Jan 2005 12:10:19 +0900, Eko Budi Setiyo \<contact\_us@haltebis.com\> writes:
