# Class: File

Inherits:
  Object
  - Object
  - File
  show all

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

## Constant Summary collapse

RELATIVE_PARENTDIR =

      '..'

RELATIVE_SAMEDIR =

      '.'

## Manipulating Paths collapse

- .cleanpath(path, rel_root = false) ⇒ String

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

- .relative_path(from, to) ⇒ String

  Turns a path `to` into a relative path from starting point `from`.

## Reading Files collapse

- .open!(file, *args, &block) ⇒ Object

  Forces opening a file (for writing) by first creating the file’s directory.

- .read_binary(file) ⇒ String

  Reads a file with binary encoding.

## Class Method Details

### .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

    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

### .open!(file, *args, &block) ⇒ `Object`

Forces opening a file (for writing) by first creating the file’s directory

Parameters:

- file (`String`) —

  the filename to open

Since:

- 0.5.2

    57
    58
    59
    60
    61

    # File 'lib/yard/core_ext/file.rb', line 57

    def self.open!(file, *args, &block)
      dir = dirname(file)
      FileUtils.mkdir_p(dir) unless directory?(dir)
      open(file, *args, &block)
    end

### .read_binary(file) ⇒ `String`

Reads a file with binary encoding

Returns:

- (`String`) —

  the ascii-8bit encoded data

Since:

- 0.5.3

    66
    67
    68

    # File 'lib/yard/core_ext/file.rb', line 66

    def self.read_binary(file)
      File.open(file, 'rb', &:read)
    end

### .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