Goal is to replace ‘byebye’ with ‘Hello ‘
# START: file.txt contains string "byebye World!"
fd = File.open('file.txt', 'a')
fd.seek(0, IO::SEEK_SET)
fd.puts 'Hello '
fd.close
# END: file.txt contains two lines "byebye World " and "Hello "
I understand what exists other excellent ( and working:) ) ways to achieve it, but why that code don’t write on right position?
I think you’re misunderstanding w+, you may want r+ as the modestring, here’s what w+ does.
truncate existing file to zero length is eating your string.