Ruby:如何计算相对于另一条路径的路径?

Ruby:如何计算相对于另一条路径的路径?,ruby,relative-path,Ruby,Relative Path,比如说,我知道我要开始的绝对路径和我要尝试达到的绝对路径: first = '/first/path' second = '/second/path' 现在我想弄清楚如何构造一条相对于第一条的路径。例如: # answer should be /first/path/../../second/path path = second.get_path_relative_to(first) 我如何在Ruby中做这类事情?使用: require 'pathname' first = Pathnam

比如说,我知道我要开始的绝对路径和我要尝试达到的绝对路径:

first = '/first/path'
second = '/second/path'
现在我想弄清楚如何构造一条相对于第一条的路径。例如:

# answer should be /first/path/../../second/path
path = second.get_path_relative_to(first)
我如何在Ruby中做这类事情?

使用:

require 'pathname'

first = Pathname.new '/first/path'
second = Pathname.new '/second/path'

relative = second.relative_path_from first
# ../../second/path

first + relative
# /second/path