wrong File::SEPARATOR ?

Discussion in 'Ruby' started by Ronald Fischer, Sep 14, 2007.

  1. On my Ruby 1.8.5 under Windows, I have:

    C:> ruby -e 'puts File::SEPARATOR'=20
    /

    Shouldn't it output '\' instead?

    Ronald
    --=20
    Ronald Fischer <>
    Phone: +49-89-452133-162
     
    Ronald Fischer, Sep 14, 2007
    #1
    1. Advertising

  2. Ronald Fischer

    Dan Zwell Guest

    Ronald Fischer wrote:
    > On my Ruby 1.8.5 under Windows, I have:
    >
    > C:> ruby -e 'puts File::SEPARATOR'
    > /
    >
    > Shouldn't it output '\' instead?
    >
    > Ronald


    Nope, this variable correct because within ruby and its core libraries,
    everything can open files with forward slash separated paths. Try it. It
    is probably more reliable than using paths with backslashes (no
    potential for confusing the file separator with the escape character).
    Now, if you need to pass the path to a non-ruby app, then you will need
    to convert them to backslashes.

    Dan
     
    Dan Zwell, Sep 14, 2007
    #2
    1. Advertising

  3. > Nope, this variable correct because within ruby and its core=20
    > libraries,=20
    > everything can open files with forward slash separated paths.=20
    > Try it.


    I know, and that's what I am doing anyway. I just thought - after
    reading about File::SEPARATOR - that this would be a good way=20
    to pass path parameters on to external processes.

    So this is not OS dependend, and within *every* Ruby implementation
    it is supposed to be a slash ... not that much sense defining
    it as a named constant then, isn't it?

    Ronald
     
    Ronald Fischer, Sep 14, 2007
    #3
  4. Ronald Fischer

    Dan Zwell Guest

    Ronald Fischer wrote:
    >> Nope, this variable correct because within ruby and its core
    >> libraries,
    >> everything can open files with forward slash separated paths.
    >> Try it.

    >
    > I know, and that's what I am doing anyway. I just thought - after
    > reading about File::SEPARATOR - that this would be a good way
    > to pass path parameters on to external processes.
    >
    > So this is not OS dependend, and within *every* Ruby implementation
    > it is supposed to be a slash ... not that much sense defining
    > it as a named constant then, isn't it?
    >
    > Ronald
    >
    >


    You have a point, as I just can't imagine that the file separator used
    by Ruby internally will change any time soon (but who knows?). Thinking
    about valid use cases, I tried changing the value of this constant, only
    to find that File.joint doesn't use the value:

    >> File::SEPARATOR = "\\"

    (irb):3: warning: already initialized constant SEPARATOR
    => "\\"
    >> File.join("c:", "My Documents")

    => "c:/My Documents"
    >> File::SEPARATOR

    => "\\"

    I would have expected the result to be "c:\\My Documents"

    So the best use case (in my opinion) of this variable does not work.
    This behavior is in direct contradiction with the documentation for
    File.join, by the way (Ruby 1.8.6).

    I wonder whether this has been fixed in 1.9? It really does seem a bug
    that File.join does not use File::SEPARATOR (when the docs say it does).
    I looked at the c code of this function, and it does use a variable
    called "separator", but it seems changing File::SEPARATOR does not
    change the c variable.

    Dan
     
    Dan Zwell, Sep 14, 2007
    #4
  5. On Sep 14, 2007, at 5:12 AM, Dan Zwell wrote:

    > Ronald Fischer wrote:
    >> On my Ruby 1.8.5 under Windows, I have:
    >> C:> ruby -e 'puts File::SEPARATOR' /
    >> Shouldn't it output '\' instead?
    >> Ronald

    >
    > Nope, this variable correct because within ruby and its core
    > libraries, everything can open files with forward slash separated
    > paths. Try it.


    The truth is that Windows itself understands / as a path separator.
    It's just that many interfaces, like the DOS shells, do not.

    James Edward Gray II
     
    James Edward Gray II, Sep 14, 2007
    #5
  6. Ronald Fischer

    Bill Kelly Guest

    From: "James Edward Gray II" <>
    > On Sep 14, 2007, at 5:12 AM, Dan Zwell wrote:
    >
    >> Ronald Fischer wrote:
    >>> On my Ruby 1.8.5 under Windows, I have:
    >>> C:> ruby -e 'puts File::SEPARATOR' /
    >>> Shouldn't it output '\' instead?


    Windows ruby does define ALT_SEPARATOR:

    >> File::ALT_SEPARATOR

    => "\\"

    (Really a single backslash, self-escaped in the inspect.)


    >> Nope, this variable correct because within ruby and its core
    >> libraries, everything can open files with forward slash separated
    >> paths. Try it.

    >
    > The truth is that Windows itself understands / as a path separator.
    > It's just that many interfaces, like the DOS shells, do not.


    Just a potential word of caution going forward... I seem to
    recall reading on this list awhile back that the Unicode
    file API's in Windows *do not* accept forward slashes for
    path separators.

    (If that's true, then this may be an issue if Unicode filename
    support is ever added to win32 ruby... Although I suppose the
    conversion from / to \ could be handled behind the scenes.)


    Regards,

    Bill
     
    Bill Kelly, Sep 14, 2007
    #6
  7. > You have a point, as I just can't imagine that the file=20
    > separator used=20
    > by Ruby internally will change any time soon (but who=20
    > knows?).=20


    I think this can be ruled out, because there probably won't be
    too many apps around which just *know* that Ruby accepts /, and
    have slashes hardcoded everywhere. I too have never thought about
    the existence of File::SEPARATOR as a constant, and came accross
    it only when I wanted to construct file names for external applications,
    which should be OS independent.

    Ronald
     
    Ronald Fischer, Sep 17, 2007
    #7
  8. > The truth is that Windows itself understands / as a path separator. =20
    > It's just that many interfaces, like the DOS shells, do not.


    And that's exactly the problem: When shelling out to external programs,
    and / is there usually interpreted as leading in an option (like "-"
    does in Posix like systems).

    Ronald
     
    Ronald Fischer, Sep 17, 2007
    #8
  9. > Windows ruby does define ALT_SEPARATOR:
    >=20
    > >> File::ALT_SEPARATOR

    > =3D> "\\"


    Is it guaranteed to also exist on Linux or Mac Ruby, having the
    value '/' on those systems?

    Ronald
    --=20
    Ronald Fischer <>
    Phone: +49-89-452133-162
     
    Ronald Fischer, Sep 17, 2007
    #9
  10. Ronald Fischer

    Eric Hodel Guest

    On Sep 17, 2007, at 01:23, Ronald Fischer wrote:
    >> Windows ruby does define ALT_SEPARATOR:
    >>
    >>>> File::ALT_SEPARATOR

    >> => "\\"

    >
    > Is it guaranteed to also exist on Linux or Mac Ruby, having the
    > value '/' on those systems?


    Those systems don't have an alternate separator so it is set to nil.

    --
    Poor workers blame their tools. Good workers build better tools. The
    best workers get their tools to do the work for them. -- Syndicate Wars
     
    Eric Hodel, Sep 17, 2007
    #10
  11. Ronald Fischer

    Phlip Guest

    Ronald Fischer wrote:

    > On my Ruby 1.8.5 under Windows, I have:


    C:> ruby -e 'puts File::SEPARATOR'
    /

    > Shouldn't it output '\' instead?


    Not sure if this is answered yet, but Windows accepts /. That prevents
    endless hacks in all the legacy code that Windows must support.

    Now all we need is a Notepad.exe that, between getting every encoding in the
    world correct, will respectfully display a lowly \n correctly...

    --
    Phlip
     
    Phlip, Sep 26, 2007
    #11
    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. juicy

    file separator

    juicy, Dec 6, 2004, in forum: Java
    Replies:
    4
    Views:
    454
    juicy
    Dec 14, 2004
  2. Lee Sander
    Replies:
    6
    Views:
    300
    Hendrik van Rooyen
    Jun 1, 2007
  3. Steve Howell
    Replies:
    3
    Views:
    288
    George Sakkis
    Jun 2, 2007
  4. Replies:
    2
    Views:
    261
  5. Matt Garrish
    Replies:
    9
    Views:
    169
    Matt Garrish
    Jan 17, 2006
Loading...

Share This Page