OS independent way to read/write paths?

(Berger, Daniel) #1

See pathname2, available on the RAA. It will preserve your slashes as
is. It is also much better suited for Windows.

Regards,

Dan

···

-----Original Message-----
From: John Wells [mailto:lists@sourceillustrated.com]
Sent: Tuesday, August 23, 2005 6:11 AM
To: ruby-talk ML
Subject: OS independent way to read/write paths?

Guys,

I'm running into some problems running scripts on Windows
that do file path mangling. Problem is that it appears Ruby
converts all windows paths to forward slashes as opposed to
back slashes...fine by me, but I need to write them out on
occasion for consumption by other Windows programs or batch
files, and then I have a problem.

I know this would be a relatively simple thing to handle on
my own, but wanted to know if, by my newness to Ruby, I'm
missing a wheel that's already been invented. What's the
idiomatic way of handling this sort of problem?

Thanks!
John

(Christopher Aldridge) #2

to determine how to handle the path, I use something as such:

require 'rbconfig'
include Config

home = "c:/documents and settings/username" if CONFIG['host'].match("mswin")
home = "/home/username" if CONFIG['host'].match("solaris") ##for
solaris nix box

system("echo hello > #{home}/.txt")

The forward slashes usually work for windows in most cases but....
hope this helps..

···

On 8/23/05, Berger, Daniel <Daniel.Berger@qwest.com> wrote:

> -----Original Message-----
> From: John Wells [mailto:lists@sourceillustrated.com]
> Sent: Tuesday, August 23, 2005 6:11 AM
> To: ruby-talk ML
> Subject: OS independent way to read/write paths?
>
>
> Guys,
>
> I'm running into some problems running scripts on Windows
> that do file path mangling. Problem is that it appears Ruby
> converts all windows paths to forward slashes as opposed to
> back slashes...fine by me, but I need to write them out on
> occasion for consumption by other Windows programs or batch
> files, and then I have a problem.
>
> I know this would be a relatively simple thing to handle on
> my own, but wanted to know if, by my newness to Ruby, I'm
> missing a wheel that's already been invented. What's the
> idiomatic way of handling this sort of problem?
>
> Thanks!
> John

See pathname2, available on the RAA. It will preserve your slashes as
is. It is also much better suited for Windows.

Regards,

Dan

(Berger, Daniel) #3

Christopher Aldridge wrote:

to determine how to handle the path, I use something as such:

require 'rbconfig'
include Config

home = "c:/documents and settings/username" if CONFIG['host'].match("mswin")
home = "/home/username" if CONFIG['host'].match("solaris") ##for
solaris nix box

system("echo hello > #{home}/.txt")

The forward slashes usually work for windows in most cases but....
hope this helps..

I'm going OT here, but...

First, you don't need rbconfig. Just check against PLATFORM, e.g. if PLATFORM.match("mswin").

Second, I would use ENV["HOME"] || ENV["USERPROFILE"] over a hard coded home directory on Windows.

Third, on Unix platforms, you can generally rely on ENV["HOME"]. If not, you can always use the etc or sys-admin package, e.g.

Admin.get_user(Admin.get_login).dir

Actually, you can do the same thing on Windows, though it usually isn't set locally (and you probably don't want the global home directory), e.g.

Admin.get_user(Admin.get_login).home_dir

Regards,

Dan

(Nobuyoshi Nakada) #4

Hi,

At Thu, 25 Aug 2005 06:42:07 +0900,
Daniel Berger wrote in [ruby-talk:153508]:

Second, I would use ENV["HOME"] || ENV["USERPROFILE"] over a hard coded home
directory on Windows.

In 1.9, ENV["HOME"] will be set to %HOMEDRIVE%%HOMEPATH%,
%USERPROFILE%, or Personal Folder, at the start up. But now I
noticed it is not in 1.8, should it be backported?

···

--
Nobu Nakada

(Christopher Aldridge) #5

Cool! Thanks for sharing the correct way of doing it :slight_smile:

···

On 8/24/05, Daniel Berger <Daniel.Berger@qwest.com> wrote:

Christopher Aldridge wrote:
> to determine how to handle the path, I use something as such:
>
> require 'rbconfig'
> include Config
>
> home = "c:/documents and settings/username" if CONFIG['host'].match("mswin")
> home = "/home/username" if CONFIG['host'].match("solaris") ##for
> solaris nix box
>
> system("echo hello > #{home}/.txt")
>
> The forward slashes usually work for windows in most cases but....
> hope this helps..

I'm going OT here, but...

First, you don't need rbconfig. Just check against PLATFORM, e.g. if
PLATFORM.match("mswin").

Second, I would use ENV["HOME"] || ENV["USERPROFILE"] over a hard coded home
directory on Windows.

Third, on Unix platforms, you can generally rely on ENV["HOME"]. If not, you
can always use the etc or sys-admin package, e.g.

Admin.get_user(Admin.get_login).dir

Actually, you can do the same thing on Windows, though it usually isn't set
locally (and you probably don't want the global home directory), e.g.

Admin.get_user(Admin.get_login).home_dir

Regards,

Dan