# How to solve Windows PATH in Ruby?

**URL:** <https://www.ruby-forum.com/t/how-to-solve-windows-path-in-ruby/100137>\
**Category:** Ruby\
**Created:** [July 27, 2006, 10:56am UTC](https://www.ruby-forum.com/t/how-to-solve-windows-path-in-ruby/100137 "2006-07-27T10:56:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zdk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/9d8465/32.png) [@zdk](https://www.ruby-forum.com/u/zdk)\
**Post date:** [July 27, 2006, 10:56am UTC](https://www.ruby-forum.com/t/how-to-solve-windows-path-in-ruby/100137/1 "2006-07-27T10:56:32Z")

</div>

require ‘pathname’  
p = Pathname.new(“C:\ruby\samples\myFile”)  
dir = p.dirname  
base = p.basename  
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:** ![zdk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/9d8465/32.png) [@zdk](https://www.ruby-forum.com/u/zdk)\
**Post date:** [July 27, 2006, 11:18am UTC](https://www.ruby-forum.com/t/how-to-solve-windows-path-in-ruby/100137/2 "2006-07-27T11:18:26Z")

</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.

On 7/27/06, zdk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘pathname’  
> p = Pathname.new(“C:\ruby\samples\myFile”)

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

---

<div class="post-metadata">

**Author:** ![zdk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/9d8465/32.png) [@zdk](https://www.ruby-forum.com/u/zdk)\
**Post date:** [July 28, 2006, 9:54am UTC](https://www.ruby-forum.com/t/how-to-solve-windows-path-in-ruby/100137/3 "2006-07-28T09:54:39Z")

</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

---

<div class="post-metadata">

**Author:** ![zdk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/9d8465/32.png) [@zdk](https://www.ruby-forum.com/u/zdk)\
**Post date:** [July 28, 2006, 5:34am UTC](https://www.ruby-forum.com/t/how-to-solve-windows-path-in-ruby/100137/4 "2006-07-28T05:34:11Z")

</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:** ![zdk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/9d8465/32.png) [@zdk](https://www.ruby-forum.com/u/zdk)\
**Post date:** [July 29, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/how-to-solve-windows-path-in-ruby/100137/5 "2006-07-29T15:26:31Z")

</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:

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

Curt
