Pathname#relative_path_from questions

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs