# How to solve Windows PATH in Ruby?

**URL:** <https://rubytalk.org/t/how-to-solve-windows-path-in-ruby/29340>\
**Category:** ruby-talk\
**Created:** [27 July 2006 08:55 UTC](https://rubytalk.org/t/how-to-solve-windows-path-in-ruby/29340 "2006-07-27T08:55:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zdk](https://avatars.discourse-cdn.com/v4/letter/z/9d8465/32.png) [@zdk](https://rubytalk.org/u/zdk)\
**Post date:** [27 July 2006 08:55 UTC](https://rubytalk.org/t/how-to-solve-windows-path-in-ruby/29340/1 "2006-07-27T08:55:04Z")

</div>

require 'pathname'  
&nbsp;&nbsp;p = Pathname.new("C:\ruby\samples\myFile")  
&nbsp;&nbsp;dir = p.dirname  
&nbsp;&nbsp;base = p.basename  
&nbsp;&nbsp;puts base

I just like to use "myFile",but p.basename seems not work on Windows  
Platform.

How to solve this kind of problem?

any help or suggest would be greatly appreciated much.  
Thanks a lot.

Warachet S.

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [27 July 2006 09:14 UTC](https://rubytalk.org/t/how-to-solve-windows-path-in-ruby/29340/2 "2006-07-27T09:14:51Z")

</div>

Hi,

just escape your backlashes, or use forward slashes (/).

In my code I convert all paths tu unix style, work with them in that  
form, and just before calling windows I convert them back. It makes  
things much simpler. And in most cases even windows can handle forward  
slashes.

J.

> &nbsp;&nbsp;require 'pathname'  
> &nbsp;&nbsp;p = Pathname.new("C:\ruby\samples\myFile")

p = Pathname.new("C:\\ruby\\samples\\myFile")

> **···**
>
> On 7/27/06, zdk \<nx2zdk@gmail.com\> wrote:
> 
> > &nbsp;&nbsp;dir = p.dirname  
> > &nbsp;&nbsp;base = p.basename  
> > &nbsp;&nbsp;puts base
> > 
> > I just like to use "myFile",but p.basename seems not work on Windows  
> > Platform.
> > 
> > How to solve this kind of problem?
> > 
> > any help or suggest would be greatly appreciated much.  
> > Thanks a lot.
> > 
> > Warachet S.

---

<div class="post-metadata">

**Author:** ![N\_Okia](https://avatars.discourse-cdn.com/v4/letter/n/fbc32d/32.png) [@N\_Okia](https://rubytalk.org/u/N_Okia)\
**Post date:** [28 July 2006 03:33 UTC](https://rubytalk.org/t/how-to-solve-windows-path-in-ruby/29340/3 "2006-07-28T03:33:12Z")

</div>

I think this also depends on what ruby you are using. If you are using  
One Click installer Ruby for Windows, then your code should work as  
you have it. If you are using cygwin, then it probably won't work  
correctly with the backslashes.

Based on the path you are referring to: C:\ruby\, I assume you are  
using One Click Ruby. However, in either event, forward slash should  
work.

---

<div class="post-metadata">

**Author:** ![131](https://avatars.discourse-cdn.com/v4/letter/1/50afbb/32.png) [@131](https://rubytalk.org/u/131)\
**Post date:** [28 July 2006 07:52 UTC](https://rubytalk.org/t/how-to-solve-windows-path-in-ruby/29340/4 "2006-07-28T07:52:13Z")

</div>

Hi,

Why bothrer and remember which slash to use ? Ruby does it for you.  
Here is my approach:

require 'pathname'  
p = Pathname.new(File.join('c:', 'ruby', 'samples', 'myfile'))  
=\> #\<Pathname:c:/ruby/samples/myfile\>  
puts p.dirname  
=\>c:/ruby/samples  
puts p.basename  
=\> myfile

File.join just cancatenates all strings with appropriate path delimiter.  
And AFAIK ruby internally always uses / slash (e.g. puts dirname will  
print c:/ruby/samples also on windows).

> **···**
>
> --  
> Martins
> 
> On 7/28/06, N Okia \<wrecklass1@gmail.com\> wrote:
> 
> > I think this also depends on what ruby you are using. If you are using  
> > One Click installer Ruby for Windows, then your code should work as  
> > you have it. If you are using cygwin, then it probably won't work  
> > correctly with the backslashes.
> > 
> > Based on the path you are referring to: C:\ruby\, I assume you are  
> > using One Click Ruby. However, in either event, forward slash should  
> > work.

---

<div class="post-metadata">

**Author:** ![Curt\_Work\_Hibbs1](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@Curt\_Work\_Hibbs1](https://rubytalk.org/u/Curt_Work_Hibbs1)\
**Post date:** [29 July 2006 13:24 UTC](https://rubytalk.org/t/how-to-solve-windows-path-in-ruby/29340/5 "2006-07-29T13:24:37Z")

</div>

If you're going to use blackslashes in a literal string you need to  
double them because the backslash is an escape character. So, you  
really need to do this:

&nbsp;&nbsp;p = Pathname.new("C:\\ruby\\samples\\myFile")

Curt

> **···**
>
> On 7/27/06, N Okia \<wrecklass1@gmail.com\> wrote:
> 
> > I think this also depends on what ruby you are using. If you are using  
> > One Click installer Ruby for Windows, then your code should work as  
> > you have it. If you are using cygwin, then it probably won't work  
> > correctly with the backslashes.
> > 
> > Based on the path you are referring to: C:\ruby\, I assume you are  
> > using One Click Ruby. However, in either event, forward slash should  
> > work.
