J
James Dinkel
Here is my current method for editing files:
<code>
filename = File.join('path', 'to', 'file')
content = []
File.open(filename, 'r') do |file|
content = file.readlines
end
content.collect! do |line|
line.gsub!(/six/, "half a dozen")
line.chomp
end
File.open(filename, 'w') do |file|
file.write content.join($/)
end
</code>
This is expensive though, as it rewrites the entire file, even if only a
single word in the whole file is changed. I'm wondering if there is a
better way to do this.
James
<code>
filename = File.join('path', 'to', 'file')
content = []
File.open(filename, 'r') do |file|
content = file.readlines
end
content.collect! do |line|
line.gsub!(/six/, "half a dozen")
line.chomp
end
File.open(filename, 'w') do |file|
file.write content.join($/)
end
</code>
This is expensive though, as it rewrites the entire file, even if only a
single word in the whole file is changed. I'm wondering if there is a
better way to do this.
James