I have a simple script that does some search and replace.
This is basically it:
File.open("us_cities.yml", "r+") do |file|
while line = file.gets
"do find a replace"
end
"Here I want to write to a new file"
end
As you can see I want to write a new file with the output. How can I do this?
Outputting to a new file can be done like this (don’t forget the second parameter):
So in your example, you could do this :
If you want to append to the file, make sure you put the opening of the output file outside of the loop.