[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Normalize Windows Path Names

Brad Tilley

4/6/2006 12:52:00 PM

Is there a way to normalize pathnames? On Windows, many system calls
return a windows path that contains back slashes like this:

C:\windows\system32

I'd prefer to have forward slashes since they are easier to work with
like this:

C:/windows/system32

Windows will accept either slash. What is the best way for Ruby to
convert to normal slashes?

Thank you,
Brad
2 Answers

Daniel Berger

4/7/2006 12:27:00 AM

0

rtilley wrote:
> Is there a way to normalize pathnames? On Windows, many system calls
> return a windows path that contains back slashes like this:
>
> C:\windows\system32
>
> I'd prefer to have forward slashes since they are easier to work with
> like this:
>
> C:/windows/system32
>
> Windows will accept either slash. What is the best way for Ruby to
> convert to normal slashes?

See String#tr and String#tr!

"C:\\foo\\bar".tr("\\", '/') # C:/foo/bar

Regards,

Dan

Brad Tilley

4/7/2006 12:52:00 PM

0

Daniel Berger wrote:
> rtilley wrote:
>
>>Is there a way to normalize pathnames? On Windows, many system calls
>>return a windows path that contains back slashes like this:
>>
>>C:\windows\system32
>>
>>I'd prefer to have forward slashes since they are easier to work with
>>like this:
>>
>>C:/windows/system32
>>
>>Windows will accept either slash. What is the best way for Ruby to
>>convert to normal slashes?
>
>
> See String#tr and String#tr!
>
> "C:\\foo\\bar".tr("\\", '/') # C:/foo/bar
>
> Regards,
>
> Dan

Thanks Dan! That's great.