Forum: Ruby difference in file.join between Linux, win32?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-01-31 02:58
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
84ae88b11c1c8da98ae1f229653ee756?d=identicon&s=25 Tony Lawetta (Guest)
on 2009-01-31 20:35
(Received via mailing list)
Not sure how this question is related to File.join().

Roger Pack 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.
Ff97ca87af59ee68ceff5877a8365788?d=identicon&s=25 Jarmo Pertman (juuser)
on 2009-01-31 20:41
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 Pack wrote:
> in windows
> => .
E7cff3cfd41c495e1012227d7dc24202?d=identicon&s=25 Luis Lavena (luislavena)
on 2009-01-31 21:45
(Received via mailing list)
On Jan 31, 5:41 pm, Jarmo Pertman <jarm...@gmail.com> 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
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2009-01-31 22:47
> 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
Bc368ef524130e8d0deb386de961e24a?d=identicon&s=25 Suraj Kurapati (snk)
on 2009-02-01 03:44
Roger Pack 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.
This topic is locked and can not be replied to.