Method: File.relative_path

Defined in:
lib/yard/core_ext/file.rb

.relative_path(from, to) ⇒ String

Turns a path to into a relative path from starting point from. The argument from is assumed to be a filename. To treat it as a directory, make sure it ends in File::SEPARATOR (‘/’ on UNIX filesystems).

Parameters:

  • from (String)

    the starting filename (or directory with from_isdir set to true).

  • to (String)

    the final path that should be made relative.

Returns:

  • (String)

    the relative path from from to to.



19
20
21
22
23
24
25
26
27
28
# File 'lib/yard/core_ext/file.rb', line 19

def self.relative_path(from, to)
  from = expand_path(from).split(SEPARATOR)
  to = expand_path(to).split(SEPARATOR)
  from.length.times do
    break if from[0] != to[0]
    from.shift; to.shift
  end
  from.pop
  join(*(from.map { RELATIVE_PARENTDIR } + to))
end