假设我知道我开始的绝对路径和我试图到达的绝对路径:
first = '/first/path'
second = '/second/path'
现在我想弄清楚如何构建一个相对于第一个路径。例如:
# answer should be /first/path/../../second/path
path = second.get_path_relative_to(first)
我怎样才能在 Ruby 中做这种事情?
假设我知道我开始的绝对路径和我试图到达的绝对路径:
first = '/first/path'
second = '/second/path'
现在我想弄清楚如何构建一个相对于第一个路径。例如:
# answer should be /first/path/../../second/path
path = second.get_path_relative_to(first)
我怎样才能在 Ruby 中做这种事情?
使用Pathname#relative_path_from
:
require 'pathname'
first = Pathname.new '/first/path'
second = Pathname.new '/second/path'
relative = second.relative_path_from first
# ../../second/path
first + relative
# /second/path