# File path operations?

**URL:** <https://www.ruby-forum.com/t/file-path-operations/149225>\
**Category:** Ruby\
**Created:** [September 22, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/file-path-operations/149225 "2008-09-22T17:56:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 22, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/file-path-operations/149225/1 "2008-09-22T17:56:26Z")

</div>

Ruby has an abstract File/path type which is nice (I’m mainly a Python  
user and it doesn’t), but it doesn’t seem to provide object-oriented  
methods to combine file paths, i.e. I can’t go path3 = path1/path2 or  
something similar. Instead, I have to use the join function, which is  
much less elegant, IMHO. Am I missing something. Has someone written  
an extension to File that adds such path operations?

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 22, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/file-path-operations/149225/2 "2008-09-22T18:18:44Z")

</div>

2008/9/22 Kenneth McDonald [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Ruby has an abstract File/path type which is nice (I’m mainly a Python user  
> and it doesn’t), but it doesn’t seem to provide object-oriented methods to  
> combine file paths, i.e. I can’t go path3 = path1/path2 or something  
> similar. Instead, I have to use the join function, which is much less  
> elegant, IMHO. Am I missing something. Has someone written an extension to  
> File that adds such path operations?

irb(main):001:0\> require ‘pathname’  
=\> true  
irb(main):002:0\> pn = Pathname.new ‘.’  
=\> #Pathname:.  
irb(main):003:0\> pn.absolute?  
=\> false  
irb(main):004:0\> pn + “foo” + “bar”  
=\> #Pathname:foo/bar

Cheers

robert

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 22, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/file-path-operations/149225/3 "2008-09-22T18:19:19Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 22, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/file-path-operations/149225/4 "2008-09-22T20:15:35Z")

</div>

I went for years without knowing about Pathname. It seems rather  
underused.

The singleton methods File.basename, File.exist?, etc. are awkward  
because the proper abstraction is a path, not an arbitrary string.  
Moreover, these methods cut against the natural flow of ruby code.  
Compare

File.join File.dirname(foo), “intermediate”, File.basename(foo)

with

foo.dirname.join “intermediate”, foo.basename

I would like to see more ruby packages use Pathname. If your method  
takes a string argument, then use it as a string. However if your  
method takes a path name disguised as a string, consider taking a  
Pathname. (In practice I expect you’d convert any non-Pathnames to  
Pathnames.)

Now that Pathname has been in the standard library since 1.8.0, perhaps  
it is time for the whole standard library to use it? It certainly makes  
code cleaner and clearer.

It also discourages the use of problematic “#{x}/#{y}” constructs. If x  
is nil, you are now pointing at the root directory, with not a peep of  
warning from ruby.
