Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Why does File.join use "/" on Windows?

Reply
Thread Tools

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

 
 
Bret Pettichord
Guest
Posts: n/a
 
      03-30-2007
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

 
Reply With Quote
 
 
 
 
Wang Dong
Guest
Posts: n/a
 
      03-30-2007
On 3月30日, 上午9时21分, "Bret Pettichord" <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
 
John W. Kennedy
Guest
Posts: n/a
 
      03-30-2007
Wang Dong wrote:
> On 3月30日, 上午9时21分, "Bret Pettichord" <(E-Mail Removed)> wrote:
>> 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.


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
 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      03-30-2007

On Mar 29, 2007, at 10:35 PM, Wang Dong wrote:

> On 3=D4=C230=C8=D5, =C9=CF=CE=E79=CA=B121=B7=D6, "Bret Pettichord" =

<(E-Mail Removed)> =20
> wrote:
>> 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"
>> =3D> "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.


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:

>> File.new('/tmp/foo','w').to_s(true)

=3D> "/tmp/foo"
>> File.new('/tmp/../tmp/./foo','w').to_s(true)

=3D> "/tmp/foo"
>> File.new('/tmp/../tmp/./foo','w').to_s

=3D> "/tmp/../tmp/./foo"

Or presumably on Windows:
>> File.new(File.join('foo','bar'),'w').to_s(true)

=3D> "C:\\Users\\bret\\foo\\bar"
>> File.new(File.join('foo','bar'),'w').to_s

=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
http://www.velocityreviews.com/forums/(E-Mail Removed)




 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-30-2007
On 30.03.2007 03:21, Bret Pettichord wrote:
> 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
 
Reply With Quote
 
Wayne Vucenic
Guest
Posts: n/a
 
      04-02-2007
On 3/30/07, Robert Klemme <(E-Mail Removed)> wrote:
> 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

---
Wayne Vucenic
No Bugs Software
Ruby, Erlang and C# Agile Contract Programming in Silicon Valley

 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      04-02-2007
On Mar 30, 3:07 pm, Robert Klemme <(E-Mail Removed)> wrote:
> On 30.03.2007 03:21, Bret Pettichord wrote:
>
> > 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

 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      04-04-2007
On 4/2/07, Daniel Berger <(E-Mail Removed)> wrote:

> Wuby always uses backslashes.

\T\h\e\n\ \\\/\\\/\o\u\l\d\n\'\t it be \\\/\\\/\u\b\y <G>
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 03-09-2011 11:00 PM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM



Advertisments