LoginSignup
15
13

More than 5 years have passed since last update.

[Ruby] 絶対パス間の相対パスを作る

Posted at

自作する前に捜せば、大抵あるよということで。

path_from = Pathname('/a/b/c')
path_to = Pathname('/a/b/c/d/e')
path_to.relative_path_from(path_from).to_s
#=> "d/e"
path_to = Pathname('/a/x/y')
path_to.relative_path_from(path_from).to_s
#=> "../../x/y"

使えるのはRuby 1.8.1から。

15
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
13