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

Discussion in 'Ruby' started by Bret Pettichord, Mar 30, 2007.

  1. 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
     
    Bret Pettichord, Mar 30, 2007
    #1
    1. Advertising

  2. Bret Pettichord

    Wang Dong Guest

    On 3ÔÂ30ÈÕ, ÉÏÎç9ʱ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 + "\\" +
    > 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.
     
    Wang Dong, Mar 30, 2007
    #2
    1. Advertising

  3. Wang Dong wrote:
    > On 3ÔÂ30ÈÕ, ÉÏÎç9ʱ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 + "\\" +
    >> 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
     
    John W. Kennedy, Mar 30, 2007
    #3
  4. 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" =

    <> =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
     
    Rob Biedenharn, Mar 30, 2007
    #4
  5. 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
     
    Robert Klemme, Mar 30, 2007
    #5
  6. On 3/30/07, Robert Klemme <> 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
     
    Wayne Vucenic, Apr 2, 2007
    #6
  7. On Mar 30, 3:07 pm, Robert Klemme <> 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
     
    Daniel Berger, Apr 2, 2007
    #7
  8. On 4/2/07, Daniel Berger <> 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/
     
    Rick DeNatale, Apr 4, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    892
    Alan Silver
    Jun 5, 2006
  2. Jim Hefferon

    Why does the "".join(r) do this?

    Jim Hefferon, May 20, 2004, in forum: Python
    Replies:
    17
    Views:
    532
    Tim Roberts
    May 24, 2004
  3. googleboy
    Replies:
    1
    Views:
    932
    Benji York
    Oct 1, 2005
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    917
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,030
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page