Let's analyze what's going wrong here. File#any?
returns true or false, according as its block returns true for at least one of the block arguments passed. You are not returning true or false from the block and you have no interest in the question of whether the result of File#any?
is true or false. So this is clearly the wrong method.
Moreover, as you rightly say, you are writing a new (added) line, not in any way replacing the existing line.
What you want to do is cycle through the file line by line, reading each line, and:
If the line doesn't contain the target string, write the very same line
If the line does contain the target string, write the substituted line
Thus you will read the whole file and write the whole file (with or without the substitution) in its place. This approach actually involves two files, since you have already read the line before the time comes to write it.
Alternatively you could just read the whole file at once, split it into lines, look for the target and perform the substitution, join back into a single string, and write it. If the files are small (so that the strings are small), this is much the simplest way.
For the correct patterns, see https://stackoverflow.com/a/4399299/341994