Method: File.cleanpath

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

.cleanpath(path, rel_root = false) ⇒ String

Cleans a path by removing extraneous ‘..’, ‘.’ and ‘/’ characters

Examples:

Clean a path

File.cleanpath('a/b//./c/../e') # => "a/b/e"

Parameters:

  • path (String)

    the path to clean

  • rel_root (Boolean) (defaults to: false)

    allows relative path above root value

Returns:

  • (String)

    the sanitized path

[View source]

37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yard/core_ext/file.rb', line 37

def self.cleanpath(path, rel_root = false)
  path = path.split(SEPARATOR)
  path = path.inject([]) do |acc, comp|
    next acc if comp == RELATIVE_SAMEDIR
    if comp == RELATIVE_PARENTDIR && !acc.empty? && acc.last != RELATIVE_PARENTDIR
      acc.pop
      next acc
    elsif !rel_root && comp == RELATIVE_PARENTDIR && acc.empty?
      next acc
    end
    acc << comp
  end
  File.join(*path)
end