# Path Separator and Windows

**URL:** <https://www.ruby-forum.com/t/path-separator-and-windows/51339>\
**Category:** Ruby\
**Created:** [December 29, 2005, 10:03pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339 "2005-12-29T22:03:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 29, 2005, 10:03pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/1 "2005-12-29T22:03:53Z")

</div>

Using ruby 1.8.2 on Windows XP, the path separator used for things like  
File.join is always / instead of . Is there a way to force ruby to use  
the correct path separator?

Thanks,  
Justin

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 29, 2005, 10:34pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/2 "2005-12-29T22:34:24Z")

</div>

Justin J. wrote:

> Using ruby 1.8.2 on Windows XP, the path separator used for things like  
> File.join is always / instead of . Is there a way to force ruby to use  
> the correct path separator?

“correct” is debatable, since the Windows API accepts / as a separator.

Anyway, one approach you can use is:

File.join(‘foo’,‘bar’).gsub(File::SEPARATOR,  
File::ALT\_SEPARATOR || File::SEPARATOR)

On Win32 -\> foo\bar  
On \*nix -\> foo/bar

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 29, 2005, 10:34pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/3 "2005-12-29T22:34:24Z")

</div>

On 29/12/05, Justin J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Using ruby 1.8.2 on Windows XP, the path separator used for things like  
> File.join is always / instead of . Is there a way to force ruby to use  
> the correct path separator?

Unless you’re interacting with external programs, you don’t need it to.

When you do, a simple .gsub(%r{/}) { “\” } will do.

-austin

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 29, 2005, 10:37pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/4 "2005-12-29T22:37:26Z")

</div>

On Thursday 29 December 2005 22:04, Justin J. wrote:

> Using ruby 1.8.2 on Windows XP, the path separator used for things  
> like File.join is always / instead of . Is there a way to force  
> ruby to use the correct path separator?

For ruby, “/” _is_ the correct path separator. AFAIK, ruby  
internally uses the slash as path separator on all platforms.

I recommend to use slashes and convert them to backslashes  
when external programs are called. Perhaps a library can  
help you; searching for “win” on RAA:

[http://raa.ruby-lang.org/search.rhtml?search=win](http://raa.ruby-lang.org/search.rhtml?search=win)

Kind regards,  
Stefan

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 29, 2005, 10:40pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/5 "2005-12-29T22:40:51Z")

</div>

Bob S. wrote:

> Justin J. wrote:
> 
> > Using ruby 1.8.2 on Windows XP, the path separator used for things like  
> > File.join is always / instead of . Is there a way to force ruby to use  
> > the correct path separator?
> 
> “correct” is debatable, since the Windows API accepts / as a separator.
> 
> Anyway, one approach you can use is:
> 
> File.join(‘foo’,‘bar’).gsub(File::SEPARATOR,  
> File::ALT\_SEPARATOR || File::SEPARATOR)
> 
> On Win32 -\> foo\bar  
> On \*nix -\> foo/bar

Is there a reason File::SEPARATOR isn’t set to \ to begin with for  
Windows? Even if the Windows API accepts a /, \ is standard for the  
Windows command line and executing commands with / in paths passed as  
args fails. Of course I can just do my own substitutions, but it seems  
like this could be improved.

Thanks.

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 29, 2005, 10:40pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/6 "2005-12-29T22:40:27Z")

</div>

On 29/12/05, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Justin J. wrote:
> 
> > Using ruby 1.8.2 on Windows XP, the path separator used for things like  
> > File.join is always / instead of . Is there a way to force ruby to use  
> > the correct path separator?  
> > “correct” is debatable, since the Windows API accepts / as a separator.

Sort of. The base Win32 API does accept \ or /. However, the Unicode  
Win32API – which will be used in the future – does not accept /.  
Ruby, however, should continue to support both.

-austin

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 29, 2005, 10:50pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/7 "2005-12-29T22:50:52Z")

</div>

Bob S. wrote:

> Justin J. wrote:
> 
> > Is there a reason File::SEPARATOR isn’t set to \ to begin with for  
> > Windows? Even if the Windows API accepts a /, \ is standard for the  
> > Windows command line and executing commands with / in paths passed as  
> > args fails. Of course I can just do my own substitutions, but it seems  
> > like this could be improved.
> 
> I’m not sure why it works the way it does; perhaps Matz will weigh in.  
> If you look at the source (file.c), the whole business is really quite  
> messy.
> 
> How did DOS (and then Windows) ever decide to use backslash I wonder?
> 
> And a pox on “drive letters” while we’re at it! 🙂
> 
> The Unix approach seems so much more logical to me…

No argument there. 🙂

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 29, 2005, 10:49pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/8 "2005-12-29T22:49:29Z")

</div>

Justin J. wrote:

> Is there a reason File::SEPARATOR isn’t set to \ to begin with for  
> Windows? Even if the Windows API accepts a /, \ is standard for the  
> Windows command line and executing commands with / in paths passed as  
> args fails. Of course I can just do my own substitutions, but it seems  
> like this could be improved.

I’m not sure why it works the way it does; perhaps Matz will weigh in.  
If you look at the source (file.c), the whole business is really quite  
messy.

How did DOS (and then Windows) ever decide to use backslash I wonder?

And a pox on “drive letters” while we’re at it! 🙂

The Unix approach seems so much more logical to me…

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 29, 2005, 11:43pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/9 "2005-12-29T23:43:43Z")

</div>

On Fri, 2005-12-30 at 06:48 +0900, Bob S. wrote:

> How did DOS (and then Windows) ever decide to use backslash I wonder?

By the time they decided to add support for directories to DOS, / was  
already in common use as an option specifier (similar to - on Unix).

-mental

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 30, 2005, 1:53am UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/10 "2005-12-30T01:53:23Z")

</div>

On Fri, Dec 30, 2005 at 06:48:00AM +0900, Bob S. wrote:

> How did DOS (and then Windows) ever decide to use backslash I wonder?

Depending on how you look at it, I think it was either poor planning or  
excellent planning. It’s poor planning if you think the goal was to do  
things “right”, with future cross-platform compatibility in mind. It’s  
excellent planning if you think the goal was to hinder compatibility in  
the future in the interests of fostering vendor lock-in.

–  
Chad P. [CCD CopyWrite | [http://ccd.apotheon.org](http://ccd.apotheon.org) ]

“A script is what you give the actors. A program  
is what you give the audience.” - Larry Wall

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 30, 2005, 1:50am UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/11 "2005-12-30T01:50:22Z")

</div>

On Fri, Dec 30, 2005 at 06:41:04AM +0900, Justin J. wrote:

> Is there a reason File::SEPARATOR isn’t set to \ to begin with for  
> Windows?

I’m only guessing, but I suspect it has something to do with aiming for  
cross-platform compatibility by default.

–  
Chad P. [CCD CopyWrite | [http://ccd.apotheon.org](http://ccd.apotheon.org) ]

This sig for rent: a Signify v1.14 production from

> **[Debian -- The Universal Operating System](https://www.debian.org/)**
>
> Debian is an operating system and a distribution of Free Software. It is maintained and updated through the work of many users who volunteer their time and effort.

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 30, 2005, 6:31pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/12 "2005-12-30T18:31:54Z")

</div>

Regardless of the history I am still of the opinion that something  
should change. From this thread it seems that Ruby may have chosen to  
use / everywhere to keep things consistent. Can anyone confirm that  
this really is the decision? If so, it presents the following problems  
for Windows users.

1. When a program displays paths back to users they all use / instead of  
\ which isn’t consistent with how the OS presents the paths to the user.
2. Trying to run a program in backticks for example with the paths using  
/ fails.

I think a reasonable solution would be to allow the programmer to  
configure how Ruby will behave, for example by allowing them to change a  
global variable for the path separator. I figured this would be doable  
but haven’t found a way to do so yet. I also still think it makes more  
sense to default to the line separater the OS uses, which is \ for  
Windows regardless of what the API allows.

Anyone else agree with me on this? I’m not just complaining here. I’d  
like to see Ruby improve but I want to make sure I’m not missing  
something obvious.

Thanks for all the feedback.  
Justin

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 30, 2005, 6:58pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/13 "2005-12-30T18:58:32Z")

</div>

On 30/12/05, Justin J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Regardless of the history I am still of the opinion that something  
> should change. From this thread it seems that Ruby may have chosen to  
> use / everywhere to keep things consistent. Can anyone confirm that  
> this really is the decision? If so, it presents the following problems  
> for Windows users.
> 
> 1. When a program displays paths back to users they all use / instead  
> of \ which isn’t consistent with how the OS presents the paths to  
> the user.

I don’t personally see this as an issue. I might think that having a  
method #localize for Pathname might be good that does the conversion for  
you.

> 1. Trying to run a program in backticks for example with the paths  
> using / fails.

Then backticks, %x{}, and Kernel#system should probably look for / in  
the command portion and change it to backslash.

> I think a reasonable solution would be to allow the programmer to  
> configure how Ruby will behave, for example by allowing them to change  
> a global variable for the path separator. I figured this would be  
> doable but haven’t found a way to do so yet. I also still think it  
> makes more sense to default to the line separater the OS uses, which  
> is \ for Windows regardless of what the API allows.

I disagree completely. Ruby’s paths should always use ‘/’ and the pieces  
of Ruby which interact with the command-processor and/or the filesystem  
should do the separator translation.

-austin

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 30, 2005, 7:10pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/14 "2005-12-30T19:10:42Z")

</div>

On 12/30/05, Austin Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> should do the separator translation.
> 
> -austin

I’m with Austin: Ruby paths should always use ‘/’ and the translation  
should be done by the pieces interacting with the system. This is what  
Java does, and it’s one of the nice things about Java.  
The most important benefit is that you can write programs that run on  
both Windows and Linux (and Mac and…) without the hassle of a lot of  
‘if current system is windows…’ statements.

jf

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 30, 2005, 6:22pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/15 "2005-12-30T18:22:13Z")

</div>

On 12/29/05, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> messy.
> 
> How did DOS (and then Windows) ever decide to use backslash I wonder?

To show my age :-), I think the reason is historic. The original DOS  
file  
system was stolen from CP/M. CP/M stole much of it’s syntax from DEC  
operating systems (RT-11 I think). In DEC operating systems, the ‘/’  
character was used to introduce a switch.

And a pox on “drive letters” while we’re at it! 🙂

Drive letters were also a hold over from DEC operating systems. I am not  
really criticizing the DEC operating systems. They were way ahead of the  
rest of the systems of there time until Unix arrived and changed the  
world.

The Unix approach seems so much more logical to me…

> 

As I said, Unix changed EVERYTHING.

cheers,  
Kellie

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 30, 2005, 7:36pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/16 "2005-12-30T19:36:22Z")

</div>

Austin Z. wrote:

> On 30/12/05, Justin J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Regardless of the history I am still of the opinion that something  
> > should change. From this thread it seems that Ruby may have chosen to  
> > use / everywhere to keep things consistent. Can anyone confirm that  
> > this really is the decision? If so, it presents the following problems  
> > for Windows users.
> > 
> > 1. When a program displays paths back to users they all use / instead  
> > of \ which isn’t consistent with how the OS presents the paths to  
> > the user.
> 
> I don’t personally see this as an issue. I might think that having a  
> method #localize for Pathname might be good that does the conversion for  
> you.
> 
> > 1. Trying to run a program in backticks for example with the paths  
> > using / fails.
> 
> Then backticks, %x{}, and Kernel#system should probably look for / in  
> the command portion and change it to backslash.

This seems like a reasonable solution to me.

1. Change backticks, %x{}, and Kernel#system to change slashes to  
platform’s path separator before running commands. Any other things  
need this?
2. Add a method to Pathname??? to convert to platform’s path separators  
for convenience. Anything else need this?

Without at least #1 above it seems slightly broken to me (at least not  
what one would expect), since you can have a path to an executable in a  
Pathname but then you can’t even run the command without doing a gsub on  
it’s string representation.

> > I think a reasonable solution would be to allow the programmer to  
> > configure how Ruby will behave, for example by allowing them to change  
> > a global variable for the path separator. I figured this would be  
> > doable but haven’t found a way to do so yet. I also still think it  
> > makes more sense to default to the line separater the OS uses, which  
> > is \ for Windows regardless of what the API allows.
> 
> I disagree completely. Ruby’s paths should always use ‘/’ and the pieces  
> of Ruby which interact with the command-processor and/or the filesystem  
> should do the separator translation.
> 
> -austin

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 31, 2005, 12:12pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/17 "2005-12-31T12:12:05Z")

</div>

Stefan L. wrote:

> help you; searching for “win” on RAA:
> 
> [http://raa.ruby-lang.org/search.rhtml?search=win](http://raa.ruby-lang.org/search.rhtml?search=win)
> 
> Kind regards,  
> Stefan

Except that not all Windows functions accept ‘/’ for a path separator.  
So, if you happen to be passing the result of File.join to, say,  
PathAppend using Win32API, it’s going to fail.

If you definitely need backslashes then either take Austin’s suggestion  
and use gsub, or use the pathname2 package (on the RAA), which returns  
a path with the “correct” separator. 🙂

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 31, 2005, 12:09pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/18 "2005-12-31T12:09:03Z")

</div>

Justin J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Using ruby 1.8.2 on Windows XP, the path separator used for things  
> like File.join is always / instead of . Is there a way to force  
> ruby to use the correct path separator?

AFAIK Ruby can usually use both, so ‘C:\foo\bar’ and ‘C:/foo/bar’ are  
equivalent. Btw, which Ruby version are you using? If it’s the cygwin  
version then slash definitively works,

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 31, 2005, 12:15pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/19 "2005-12-31T12:15:10Z")

</div>

On Thu, 29 Dec 2005 21:48:00 -0000, Bob S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Justin J. wrote:
> 
> > Is there a reason File::SEPARATOR isn’t set to \ to begin with for  
> > Windows? Even if the Windows API accepts a /, \ is standard for the  
> > Windows command line and executing commands with / in paths passed as  
> > args fails. Of course I can just do my own substitutions, but it seems  
> > like this could be improved.

I doubt it - they’d probably need to rewrite it (again).

Oh, sorry, you meant Ruby?

😉

> I’m not sure why it works the way it does; perhaps Matz will weigh in.  
> If you look at the source (file.c), the whole business is really quite  
> messy.
> 
> How did DOS (and then Windows) ever decide to use backslash I wonder?

Protect, _never_ share, integration is evil. Back then, anyway. Maybe  
they  
get it now.

> And a pox on “drive letters” while we’re at it! 🙂
> 
> The Unix approach seems so much more logical to me…

Me too, as I sit with my Laptop, homes and /usr/local mounted via NFS,  
music playing from /dev/cdrom and my desktop just the way I like it -  
the  
original roaming profile? AFAIR there aren’t even any symlinks over in  
winland, and local stuff _still_ gets mixed with the system…

(p.s. I don’t want to argue with any Windows users, this is just  
light-hearted jibing about a light-hearted OS ;\> Used to use DOS myself,  
time in the trenches, jumpers for goalposts, etc.)

---

<div class="post-metadata">

**Author:** ![justinj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b38774/32.png) [@justinj](https://www.ruby-forum.com/u/justinj)\
**Post date:** [December 31, 2005, 12:24pm UTC](https://www.ruby-forum.com/t/path-separator-and-windows/51339/20 "2005-12-31T12:24:15Z")

</div>

On Fri, 30 Dec 2005 17:32:07 -0000, Justin J.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anyone else agree with me on this? I’m not just complaining here. I’d  
> like to see Ruby improve but I want to make sure I’m not missing  
> something obvious.

Well, if I have to write my code to check path separators and work  
around  
non standard stuff, it isn’t going to happen. Granted using File.join  
and  
so forth is always good practice but there are often times you need to  
split a path manually or do some other kind of work on it. If all that  
gets broken then I’m just not going to even try to fix it from this end.

I don’t know, but I wonder how many others are writing code that’s  
Windows  
compatible by happy coincidence, purely because Ruby _works_ to give a  
reasonably standard environment?

Just my opinion…

[Next page](https://www.ruby-forum.com/t/path-separator-and-windows/51339.md?page=2)
