Class File
In: lib/core/facets/file/append.rb
lib/core/facets/file/create.rb
lib/core/facets/file/null.rb
lib/core/facets/file/read.rb
lib/core/facets/file/rewrite.rb
lib/core/facets/file/rootname.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/more/facets/yaml.rb
Parent: Object

Methods

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

Public Class methods

Append to a file.

CREDIT: George Moschovitis

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

Platform dependent null device.

CREDIT: Daniel Burger

Read in a file as binary data.

CREDIT: George Moschovitis

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

CREDIT: Trans

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.

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

  # Replace "foo" by "bar" in "binary"
  File.rewrite("binary", "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

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") { |str| str.reverse! }

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

Returns onlt 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

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

CREDIT: George Moschovitis

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

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

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

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

Returns the number of bytes written.

CREDIT: Gavin Sinclair

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.

Returns nil.

CREDIT: Noah Gibbs, Gavin Sinclair

Is a file a YAML file?

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 will be manditory, so in the future this can be adapted to fit that standard.

[Validate]