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

def self.open!(file, *args, &block)

Other tags:
    Since: - 0.5.2

Parameters:
  • file (String) -- the filename to open
def self.open!(file, *args, &block)
  dir = dirname(file)
  FileUtils.mkdir_p(dir) unless directory?(dir)
  open(file, *args, &block)
end

def self.read_binary(file)

Other tags:
    Since: - 0.5.3

Returns:
  • (String) - the ascii-8bit encoded data
def self.read_binary(file)
  File.open(file, 'rb', &:read)
end

def self.relative_path(from, to)

Returns:
  • (String) - the relative path from +from+ to +to+.

Parameters:
  • to (String) -- the final path that should be made relative.
  • from (String) -- the starting filename
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