Is there a way to edit each line in a file, without involving 2 files? Say, the original file has,
I want to edit it like
Tried something like this, but it replaces some of the characters.
File.open('mytest.csv', 'r+') do |file|
file.each_line do |line|
Writing it to a temporary file and then replace the original file works, However, I need to edit the file quite often and therefore prefer to do it within the file itself .Any pointers are appreciated.
You can stash the data in an array, manipulate it there, and then write it back to the file store after. Ignore the HEREDOC parts of this, and substitute your file read/write bits, and this should work:
foo = <<EOF
bar = 
foo.each_line do |line|
bar.push (line.strip + ',a')
foo = bar.join("\n")