[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Path Separator and Windows

Justin Johnson

12/29/2005 9:04:00 PM

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

--
Posted via http://www.ruby-....


19 Answers

Robert Klemme

12/29/2005 9:19:00 PM

0

Justin Johnson <justinjohnson@gmail.com> 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

Bob Showalter

12/29/2005 9:31:00 PM

0

Justin Johnson 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


Austin Ziegler

12/29/2005 9:33:00 PM

0

On 29/12/05, Justin Johnson <justinjohnson@gmail.com> 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
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Stefan Lang

12/29/2005 9:37:00 PM

0

On Thursday 29 December 2005 22:04, Justin Johnson 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?...

Kind regards,
Stefan


Austin Ziegler

12/29/2005 9:38:00 PM

0

On 29/12/05, Bob Showalter <bob_showalter@taylorwhite.com> wrote:
> Justin Johnson 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
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Justin Johnson

12/29/2005 9:41:00 PM

0

Bob Showalter wrote:
> Justin Johnson 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.

--
Posted via http://www.ruby-....


Bob Showalter

12/29/2005 9:48:00 PM

0

Justin Johnson 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...


MenTaLguY

12/29/2005 10:41:00 PM

0

On Fri, 2005-12-30 at 06:48 +0900, Bob Showalter 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

Daniel Berger

12/29/2005 11:13:00 PM

0

Stefan Lang wrote:
> On Thursday 29 December 2005 22:04, Justin Johnson 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?...
>
> 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

Chad Perrin

12/30/2005 12:49:00 AM

0

On Fri, Dec 30, 2005 at 06:41:04AM +0900, Justin Johnson 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 Perrin [ CCD CopyWrite | http://ccd.ap... ]

This sig for rent: a Signify v1.14 production from http://www.d...