9 May 2007
Here is a little ruby File class method called modify!. This simple method allows you to invoke a block on file content - array of chomped lines.
This beautiful method looks like this ;)
class File def self.modify!( filename, &block ) 'r+' ) file = open( filename, do |line| lines = file.readlines.collect! line.chompend yield lines 0 file.pos = "\n")) file.print(lines.join( file.truncate(file.pos) file.closeend end
And it can be use it like this:
File.modify!('somefile.txt') do |lines| do |line| lines.collect! "!" line.upcase+end "new line" lines << end
For now it's too small to make a gem. But maybe I'll add some similar object methods, who knows ;)