Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/rook/helper/file.rb

Overview

extends File class

Class Method Summary collapse

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

.write(filename, content) ⇒ Object

write file

ex. content = <<END foo bar END File.write('file.txt', content)



70
71
72
# File 'lib/rook/helper/file.rb', line 70

def self.write(filename, content)
  File.open(filename, 'w') { |f| f.write(content) }
end