Append to a file.
[ show source ]
# File lib/facets/core/file/self/append.rb, line 4 def File.append( file, str ) File.open( file, 'a' ) { |f| f << str } end
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)
[ show source ]
# 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
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") }
[ show source ]
# 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 in a file as binary data.
[ show source ]
# File lib/facets/core/file/self/read_binary.rb, line 4 def File.read_binary(fname) open(fname, 'rb') {|f| return f.read } end
Reads in a file, removes blank lines and remarks (lines starting with ’#’) and then returns an array of all the remaining lines.
[ show source ]
# 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
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') #=> '.'
[ show source ]
# 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
Cleans up a filename to ensure it will work on filesystem.
[ show source ]
# 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
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']
[ show source ]
# 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 a file.
[ show source ]
# File lib/facets/core/file/self/write.rb, line 4 def File.write( file, str ) File.open( file, 'w+' ) { |f| f << str } end