# Pathname#relative\_path\_from questions

**URL:** <https://www.ruby-forum.com/t/pathname-relative-path-from-questions/124793>\
**Category:** Ruby\
**Created:** [January 2, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/pathname-relative-path-from-questions/124793 "2008-01-02T18:39:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 2, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/pathname-relative-path-from-questions/124793/1 "2008-01-02T18:39:23Z")

</div>

Hi all,

Ruby 1.8.6p111

I’m curious about the result of Pathname#relative\_path\_from. Consider  
this:

require ‘pathname’  
path1 = Pathname.new(‘a’)  
path2 = Pathname.new(‘b’)

path1.relative\_path\_from(path2).to\_s =\> =\> “…/a”

My first question is philosophical. Should this method work at all  
with two relative paths? Or, in the interests of being useful instead  
of correct, should we just assume a common root?

My second question is technical. Should the result be “./a”? I mean,  
if we assume a common root, the a and b files are in the same  
directory, not the parent directory, correct?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 2, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/pathname-relative-path-from-questions/124793/2 "2008-01-02T18:49:22Z")

</div>

On Jan 2, 2008, at 12:38 PM, Daniel B. wrote:

> My second question is technical. Should the result be “./a”? I mean,  
> if we assume a common root, the a and b files are in the same  
> directory, not the parent directory, correct?

Yes, but you are looking for the relative path from path2 (“b”) to  
path1 (“a”) not for the relative path from the common root to path1.  
Two different questions, two different answers.

Think about how you would do this in the shell:

$ cd b  
$ cd …/a # this gets me to a from b

Gary W.
