Method: File.edit_text
- Defined in:
- lib/pleasant_path/file.rb
.edit_text(filename) {|text| ... } ⇒ String
Reads the file indicated by filename, and yields the entire
contents as a String to the given block for editing. Writes the
return value of the block back to the file, overwriting previous
contents. Returns the return value of the block.
47 48 49 50 51 52 53 54 55 |
# File 'lib/pleasant_path/file.rb', line 47 def self.edit_text(filename) self.open(filename, "r+") do |f| text = yield f.read f.seek(0, IO::SEEK_SET) f.write(text) f.truncate(f.pos) text end end |