mardi 28 avril 2015

How to write to a specific line in a text file using ruby

Alright, so I have a ruby file and a text file. The code is like this:

fname = "sample.txt"
somefile = File.open(fname, "a")
somefile.puts "Hello file!"
somefile.close

so what i'd like to do is instead of adding it to the end of the file, add it to a specific line. The text file looks like this:

names
kyle
andrew
joshua
devon

so what I'd like to be able to do it to insert text between "kyle" and "andrew", with each on a seperate line. Please help

Aucun commentaire:

Enregistrer un commentaire