class File

def self.cleanpath(path, rel_root = false)

Returns:
  • (String) - the sanitized path

Parameters:
  • rel_root (Boolean) -- allows relative path above root value
  • path (String) -- the path to clean

Other tags:
    Example: Clean a path -
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