Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- lib/rook/helper/file.rb
Overview
extends File class
Class Method Summary collapse
-
.edit(filename, &block) ⇒ Object
edit file content.
-
.replace(filename, &block) ⇒ Object
replace file content with the result of block.
-
.write(filename, content) ⇒ Object
write file.
Class Method Details
.edit(filename, &block) ⇒ Object
edit file content
ex.
File.edit('file.txt') do |content|
content.gsub!(/\$Release\$/, '1.0.0')
content.gsub!(/\$Copyright\$/, '(c)kuwata-lab')
content
end
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rook/helper/file.rb', line 24 def self.edit(filename, &block) File.open(filename, 'r+') do |f| content = f.read() #yield(content) result = block.arity == 1 ? yield(content) : yield(content, filename) f.rewind() f.truncate(0) #f.write(result) f.write(content) end if test(?f, filename) end |
.replace(filename, &block) ⇒ Object
replace file content with the result of block.
ex.
File.replace('file.txt') do |content|
s = content
s = s.gsub(/\$Release\$/, '1.0.0')
s = s.gsub(/\$Copyright\$/, '(c)kuwata-lab')
s
end
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rook/helper/file.rb', line 48 def self.replace(filename, &block) File.open(filename, 'r+') do |f| content = f.read() #yield(content) s = block.arity == 1 ? yield(content) : yield(content, filename) f.rewind() f.truncate(0) f.write(s) end if test(?f, filename) end |