Class | File
In: | lib/facets/core/file/self/rootname.rb
Parent: | IO

### Methods

append create null open_as_string read_binary read_list rootname sanitize split_all write yaml?

### Public Class methods

append( file, str )

Append to a file.

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)

null()
open_as_string(name, mode = "") {|str| ...}

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.

## Usage

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

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

read_binary(fname)

Read in a file as binary data.

read_list(filepath, chomp_string='')

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

rootname( file_name )

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')      #=> '.'

sanitize(filename)

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

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.

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

write( file, str )

Write a file.

yaml?(file)

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]