Why does File.join use "/" on Windows?

B

Bret Pettichord

I just had to work on some code that ran into trouble, because Ruby
uses "/" to separate folders, but the path was being passed to a
separate command (using system) that expected Window's paths to use
"\".

C:\Users\bret>irb
irb(main):001:0> File.join "foo", "bar"
=> "foo/bar"
irb(main):002:0>

I told my collegues that instead of using File.join, they should just
use + "\\" +
But this got me to wondering what the point of File.join was if you
couldn't trust it to construct paths correctly (unless you were sure
that the path was only going to be used by other ruby commands).

Is there a rationale? Is there a different library that i should be
using instead?

bret
 
W

Wang Dong

I just had to work on some code that ran into trouble, because Ruby
uses "/" to separate folders, but the path was being passed to a
separate command (using system) that expected Window's paths to use
"\".

C:\Users\bret>irb
irb(main):001:0> File.join "foo", "bar"
=> "foo/bar"
irb(main):002:0>

I told my collegues that instead of using File.join, they should just
use + "\\" +
But this got me to wondering what the point of File.join was if you
couldn't trust it to construct paths correctly (unless you were sure
that the path was only going to be used by other ruby commands).

Is there a rationale? Is there a different library that i should be
using instead?

bret

Windows NT support '/' but not win98 or DOS.
 
J

John W. Kennedy

Wang said:
Windows NT support '/' but not win98 or DOS.

No, Windows and DOS have always supported '/'. But some programs (in
particular, COMMAND.COM and CMD.EXE) have a problem because of an
ancient need to be compatible with DOS 1.0.

--
John W. Kennedy
"...if you had to fall in love with someone who was evil, I can see why
it was her."
-- "Alias"
* TagZilla 0.066 * http://tagzilla.mozdev.org
 
R

Rob Biedenharn

Windows NT support '/' but not win98 or DOS.

You could try something like this (note UNTESTED since I'm only on =20
Unixes):

class File
def to_s expand=3Dfalse
(expand ? File.expand_path(path) : path).gsub('/', SEPARATOR)
end
end

Used then like this:
=3D> "/tmp/../tmp/./foo"

Or presumably on Windows:=3D> "foo\\bar"

You could also define a String#as_file to do the same .gsub=20
('/', ::File::SEPARATOR)

If Microsoft hadn't broken the path separator in the first place, =20
they wouldn't have had to fix it. ;-)

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Robert Klemme

I just had to work on some code that ran into trouble, because Ruby
uses "/" to separate folders, but the path was being passed to a
separate command (using system) that expected Window's paths to use
"\".

C:\Users\bret>irb
irb(main):001:0> File.join "foo", "bar"
=> "foo/bar"
irb(main):002:0>

I told my collegues that instead of using File.join, they should just
use + "\\" +

I'd probably rather use ["foo", "bar"].join File::SEPARATOR (if that's
set to \\ on Windows).
But this got me to wondering what the point of File.join was if you
couldn't trust it to construct paths correctly (unless you were sure
that the path was only going to be used by other ruby commands).

Exactly. File.join produces paths that Ruby can correctly use. That's
the rationale.
Is there a rationale? Is there a different library that i should be
using instead?

Well, you could do path.tr '/', '\\' before passing the path to some
other process.

Kind regards

robert
 
W

Wayne Vucenic

I'd probably rather use ["foo", "bar"].join File::SEPARATOR (if that's
set to \\ on Windows).

File::SEPARATOR is set to '/' on all platforms:

C:\>ruby -v
ruby 1.8.5 (2006-08-25) [i386-mswin32]

C:\>irb
irb(main):001:0> File::SEPARATOR
=> "/"
irb(main):002:0> File::Separator
=> "/"

Best regards,

Wayne
 
D

Daniel Berger

I just had to work on some code that ran into trouble, because Ruby
uses "/" to separate folders, but the path was being passed to a
separate command (using system) that expected Window's paths to use
"\".
C:\Users\bret>irb
irb(main):001:0> File.join "foo", "bar"
=> "foo/bar"
irb(main):002:0>
I told my collegues that instead of using File.join, they should just
use + "\\" +

I'd probably rather use ["foo", "bar"].join File::SEPARATOR (if that's
set to \\ on Windows).

Wuby always uses backslashes. :)

Regards,

Dan
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top