How to edit a specific line in a file, without using a temp file?

How to edit a specific line in a file? 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 using just 1 file.

Tried something similar to the following, but this replaces some
characters. (My intention is, if a line includes the string "my query"
then modify that line to be "my query,1")

File.open('mydata.csv', 'r+') do |file|
  file.each_line do |line|
    if line.include?"my query"
      file.seek(-line.length, IO::SEEK_CUR)
      file.puts ',1'
      break
    end
  end
end

Any suggestions are welcome.

Thank you

···

--
Posted via http://www.ruby-forum.com/.

I think using the temp file and replacing is a good way. also lets you
revert if something goes wrong. See this Stackoverflow discussion of a
similar question:

Reading all the info into memory, performing the work, then writing it
out is another way but large files can be an issue

If your worried about performance, i suggest doing some measuring to
see if its really an issue and whether your planned solution is really
better