Pathname#relative_path_from questions

D

Daniel Berger

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
 
G

Gary Wright

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 Wright
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,370
Members
48,062
Latest member
leehaan

Latest Threads

Top