Answer the question
In order to leave comments, you need to log in
How to correctly write lines in a text file?
Good afternoon. I use Ruby 2.1.2, OS FreeBSD 9.2. It is necessary to edit the text file, but I can’t do anything so that the changes are saved.
Now I'm using this code:
f = File.open("/tmp/test.conf", "a+")
f.each_line do |line|
puts line.gsub!(/\/(.*)/, "99999")
end
f.close
Answer the question
In order to leave comments, you need to log in
Your code opens the file for writing to the end of the file,
then changes each line by subline
and closes
but does not write anything to the file
File.open("/tmp/test.conf", "r") do |in|
File.open("/tmp/tmp_test.conf", "w") do |out|
in.each_line { |line| out.puts line.gsub(/\/(.*)/, "99999") }
end
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question