Class | File

In:

lib/core/facets/file/append.rb
lib/core/facets/file/create.rb
lib/core/facets/file/ext.rb
lib/core/facets/file/null.rb
lib/core/facets/file/read_binary.rb
lib/core/facets/file/read_list.rb
lib/core/facets/file/rewrite.rb
lib/core/facets/file/rootname.rb
lib/core/facets/file/sanitize.rb
lib/core/facets/file/split_all.rb
lib/core/facets/file/split_root.rb
lib/core/facets/file/write.rb
lib/core/facets/file/writelines.rb
lib/standard/facets/yaml.rb

Parent: | Object

### Methods

append create ext null read_binary read_list rewrite rewrite! rootname sanitize split_all split_root write writelines yaml?

### Public Class methods

append(file, str)

Append to a file.

CREDIT: George Moschovitis

create(path, str='', &blk)

Creates a new file, or overwrites an existing file, and writes a string into it. Can also take a block just like File#open, which is yielded after the string is writ.

      str = 'The content for the file'
      File.create('myfile.txt', str)

CREDIT: George Moschovitis

ext(filename, new_ext=nil)

Takes a file name string and returns or changes its extension.

Without a new extension argument, returns the extension of the file name. In this respect ext is like extname, but unlike extname it does not include the dot prefix.

With a new extension argument, changes the exension of the file name to the new extension and returns it.

Examples

      File.ext('file.rb')          # => 'rb'
      File.ext('file.rb', 'txt')   # => 'file.txt'
      File.ext('file.rb', '.txt')  # => 'file.txt'
      File.ext('file.rb', '')      # => 'file'

This method can be used with String#file for more object-oriented notation:

      'file.rb'.file.ext('txt')    # => 'file.txt'

CREDIT: Lavir the Whiolet

null()

Platform dependent null device.

CREDIT: Daniel Burger

read_binary(fname)

Read in a file as binary data.

Assuming we had a binary file ‘binary.dat’.

      File.read_binary('binary.dat')

CREDIT: George Moschovitis

read_list(filepath, options={})

Reads in a file, removes blank lines and removes lines starting with ’#’ and then returns an array of all the remaining lines.

Thr remark indicator can be overridden via the +:omit:+ option, which can be a regualar expression or a string that is match against the start of a line.

CREDIT: Trans

rewrite(name, mode = "") {|| ...}

Opens a file as a string and writes back the string to the file at the end of the block.

Returns the number of written bytes or `nil` if the file wasn‘t modified.

Note that the file will even be written back in case the block raises an exception.

Mode can either be "b" or "+" and specifies to open the file in binary mode (no mapping of the plattform‘s newlines to "\n" is done) or to append to it.

Assuming we had a file ‘message.txt’ and had a binary file ‘binary.dat’.

      # Reverse contents of "message.txt"
      File.rewrite("message.txt") { |str| str.reverse }

      # Replace "foo" by "bar" in "binary.dat".
      File.rewrite("binary.dat", "b") { |str| str.gsub("foo", "bar") }

IMPORTANT: The old version of this method required in place modification of the file string. The new version will write whatever the block returns instead!!!

CREDIT: George Moschovitis

rewrite!(name, mode = "") {|| ...}

In place version of rewrite. This version of method requires that the string be modified in place within the block.

      # Reverse contents of "message"
      File.rewrite("message.txt") { |str| str.reverse! }

      # Replace "foo" by "bar" in "binary"
      File.rewrite("binary.dat", "b") { |str| str.gsub!("foo", "bar") }

rootname(path)

Returns only the first portion of the directory of a file path name.

      File.rootname('lib/jump.rb')  #=> 'lib'
      File.rootname('/jump.rb')     #=> '/'
      File.rootname('jump.rb')      #=> '.'

CREDIT: Trans

sanitize(filename)

Cleans up a filename to ensure it will work on a filesystem.

      File.sanitize("yo+baby!")   #=> 'yo+baby_'
      File.sanitize(".what&up")  #=> '.what_up'

CREDIT: George Moschovitis

split_all(path)

Splits a file path into an array of individual path components. This differs from `File.split`, which divides the path into only two parts, directory path and basename.

      File.split_all("a/b/c")  #=> ['a', 'b', 'c']

CREDIT: Trans

split_root(path)

Return the head of path from the rest of the path.

      File.split_root('etc/xdg/gtk')  #=> ['etc', 'xdg/gtk']

write(path, data)

Writes the given data to the given path and closes the file. This is done in binary mode, complementing `IO.read` in standard Ruby.

      str = 'The content for the file'
      File.write('write.txt', str)

Returns the number of bytes written.

CREDIT: Gavin Sinclair

writelines(path, data)

Writes the given array of data to the given path and closes the file. This is done in binary mode, complementing `IO.readlines` in standard Ruby.

Note that `readlines` (the standard Ruby method) returns an array of lines with newlines intact, whereas `writelines` uses `puts`, and so appends newlines if necessary. In this small way, `readlines` and `writelines` are not exact opposites.

      data = ['The content', ['for the file']]
      File.writelines('writelines.txt', data)

Returns number of lines written.

CREDIT: Noah Gibbs, Gavin Sinclair

yaml?(file)

File.yaml? provides a way to check if a file is a YAML formatted file:

      File.yaml?('project.yaml')  #=> true
      File.yaml?('project.xml')   #=> false

Note this isn‘t perfect. At present it depends on the use use of an initial document separator (eg. ’—’). With YAML 1.1 the %YAML delaration is supposed to be manditory, so in the future this can be adapted to fit that standard.

[Validate]