difference in file.join between Linux, win32?

R

Roger Pack

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
 
T

Tony Lawetta

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

Roger said:
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.
 
J

Jarmo Pertman

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
 
L

Luis Lavena

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
 
R

Roger Pack

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
 
S

Suraj Kurapati

Roger said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top