# Difference in file.join between Linux, win32?

**URL:** <https://www.ruby-forum.com/t/difference-in-file-join-between-linux-win32/159066>\
**Category:** Ruby\
**Created:** [January 31, 2009, 2:58am UTC](https://www.ruby-forum.com/t/difference-in-file-join-between-linux-win32/159066 "2009-01-31T02:58:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [January 31, 2009, 2:58am UTC](https://www.ruby-forum.com/t/difference-in-file-join-between-linux-win32/159066/1 "2009-01-31T02:58:40Z")

</div>

question:

if I run some code within the following directory structure  
./substruct\_start\_and\_bootstrap\_if\_necessary.rb  
./substruct [directory]  
/config  
boot.rb

this code is within substruct\_start\_and\_bootstrap\_if\_necessary.rb [and I  
run it from the directory that file is in]:

Dir.chdir ‘substruct’  
puts File.dirname( **FILE** )

in Linux  
=\> /home/rdp/dev/ruby-benchmark-suite/rails

in windows  
=\> .

I assume this is a windows bug [if you do a chdir then File.dirname is  
off after that point].  
?

However, in Linux  
Dir.chdir ‘/’  
puts File.dirname( **FILE** )

results in ‘.’ [which is wrong at that point], too.

Thoughts?  
-=r

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [January 31, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/difference-in-file-join-between-linux-win32/159066/2 "2009-01-31T20:35:37Z")

</div>

Not sure how this question is related to File.join().

Roger P. wrote:

> Dir.chdir ‘substruct’  
> puts File.dirname( **FILE** )

What do you expect here?  
For my understanding the Dir.chdir()  
does not have an impact on the contents  
of the variable **FILE**.

I see - on both Linux and Windows - the  
‘.’ only when I am in IRB.

t.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [January 31, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/difference-in-file-join-between-linux-win32/159066/3 "2009-01-31T20:41:32Z")

</div>

Strange, it worked for me correctly under Windows XP when starting  
script right with .rb file itself, like  
D:\Projects\Ruby\>blah.rb  
D:/Projects/Ruby

But, when starting with ruby.exe:  
D:\Projects\Ruby\>ruby blah.rb  
.

I don’t know why it works like this, but you could make it working  
correctly somehow like this:  
working\_dir = File.expand\_path(File.dirname( **FILE** ))  
Dir.chdir ‘substruct’  
puts dir

Jarmo

Roger P. wrote:

> in windows  
> =\> .

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [January 31, 2009, 9:45pm UTC](https://www.ruby-forum.com/t/difference-in-file-join-between-linux-win32/159066/4 "2009-01-31T21:45:32Z")

</div>

On Jan 31, 5:41 pm, Jarmo P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Strange, it worked for me correctly under Windows XP when starting  
> script right with .rb file itself, like  
> D:\Projects\Ruby\>blah.rb  
> D:/Projects/Ruby
> 
> But, when starting with ruby.exe:  
> D:\Projects\Ruby\>ruby blah.rb

That’s because you didn’t provide the path to blah.rb

Luis@KEORE (D:\Users\Luis\Desktop)  
$ type blah.rb  
puts File.dirname( **FILE** )

Luis@KEORE (D:\Users\Luis\Desktop)  
$ ruby blah.rb  
.

Luis@KEORE (D:\Users\Luis\Desktop)  
$ ruby d:\Users\Luis\Desktop\blah.rb  
d:/Users/Luis/Desktop

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [February 1, 2009, 3:44am UTC](https://www.ruby-forum.com/t/difference-in-file-join-between-linux-win32/159066/5 "2009-02-01T03:44:26Z")

</div>

Roger P. wrote:

> on windows:
> 
> C:\dev\test\>ruby in\_root.rb  
> ./config/boot
> 
> on linux:
> 
> $ ruby in\_root.rb  
> /home/rdp/dev/test/subdir/config/boot
> 
> Is this difference expected?

No, it is _suprising_ (principle of least surprise) that, depending on  
the operating system, **FILE** would be a absolute or relative path.

Which version of Ruby does this occur with? Can you try with 1.9.1 once  
the Windows one-click installer is released?

We should definitely file a bug report about this.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [January 31, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/difference-in-file-join-between-linux-win32/159066/6 "2009-01-31T22:47:08Z")

</div>

> That’s because you didn’t provide the path to blah.rb

I’m ok with it working that way–the only weirdness is that, with these  
files:

in\_root.rb:

Dir.chdir ‘subdir’  
require ‘in\_subdir’

subdir/  
in\_subdir.rb:  
puts File.join(File.dirname( **FILE** ), ‘config’, ‘boot’)

on windows:

C:\dev\test\>ruby in\_root.rb  
./config/boot

on linux:

$ ruby in\_root.rb  
/home/rdp/dev/test/subdir/config/boot

Is this difference expected?

Thanks!  
-=r
