Methods
Public Class methods
append( file, str )

Append to a file.

# File lib/facets/core/file/self/append.rb, line 4
def File.append( file, str )
  File.open( file, 'a' ) { |f|
    f << str
  }
end
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)
# File lib/facets/core/file/self/create.rb, line 13
def File.create(path, str='', &blk)
  File.open(path, 'w') { |f|
    f << str
    blk.call(f) if blk
  }
end
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") }
# File lib/facets/core/file/self/open_as_string.rb, line 28
def File.open_as_string(name, mode = "")

  unless block_given?
    raise(ArgumentError, "Need to supply block to File.open_as_string")
  end

  if mode.is_a?(Numeric) then
    flag, mode = mode, ""
    mode += "b" if flag & File::Constants::BINARY != 0
    mode += "+" if flag & File::Constants::APPEND != 0
  else
    mode.delete!("^b+")
  end

  str = File.open(name, "r#{mode}") { |file| file.read } #rescue ""

  old_str = str.clone

  begin
    yield str
  ensure
    if old_str != str then
      File.open(name, "w#{mode}") { |file| file.write(str) }
    end
  end

end
read_binary(fname)

Read in a file as binary data.

# File lib/facets/core/file/self/read_binary.rb, line 4
def File.read_binary(fname)
  open(fname, 'rb') {|f|
    return f.read
  }
end
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.

# File lib/facets/core/file/self/read_list.rb, line 6
def File.read_list(filepath, chomp_string='')
  farr = nil
  farr = File.read(filepath).split("\n")
  farr.collect! { |line|
    l = line.strip.chomp(chomp_string)
    (l.empty? or l[0,1] == '#') ? nil : l
  }
  farr.compact
end
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')      #=> '.'
# File lib/facets/core/file/self/rootname.rb, line 12
  def self.rootname( file_name )
    i = file_name.index('/')
    if i
      r = file_name[0...i]
      r == '' ? '/' : r
    else
      '.'
    end
  end
sanitize(filename)

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

# File lib/facets/core/file/self/sanitize.rb, line 3
def File.sanitize(filename)
  filename = File.basename(filename.gsub("\\", "/")) # work-around for IE
  filename.gsub!(/[^a-zA-Z0-9\.\-\+_]/,"_")
  filename = "_#{filename}" if filename =~ /^\.+$/
  filename
end
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']
# File lib/facets/core/file/self/split_all.rb, line 8
def File.split_all(path)
  head, tail = File.split(path)
  return [tail] if head == '.' || tail == '/'
  return [head, tail] if head == '/'
  return split_all(head) + [tail]
end
write( file, str )

Write a file.

# File lib/facets/core/file/self/write.rb, line 4
def File.write( file, str )
  File.open( file, 'w+' ) { |f|
    f << str
  }
end