Ruby: How to calculate a path relative to another one?

后端 未结 1 1893
自闭症患者
自闭症患者 2021-02-03 18:38

Let\'s say I know the absolute path that I am starting from and the absolute path that I am trying to get to:

first = \'/first/path\'
second = \'/second/path\'
<         


        
1条回答
  •  花落未央
    2021-02-03 19:08

    Use 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
    

    0 讨论(0)
提交回复
热议问题