Topic: Read a file, replace some value and write it back
How to achieve that functionality? I believed that gsub! in a block will write the changes automatically, but it is not the case:
pattern = /\.(png|gif|jpg)/
locale_suffix = "_fr"
Dir.foreach(Dir.pwd ) do |filename|
unless File.directory?(filename)
File.open(filename, "r+") do |file|
while line = file.gets
match = pattern.match(line)
if(match)
line.gsub!(pattern, locale_suffix << match[0])
end
end
end
end
endOr there is the only and may be the best solution by using a temporary file for writing, and then remove the original file and rename the temporary one?
Last edited by Javix (2012-02-20 10:40:10)