Ruby file writing
I'am writing a script in Ruby to help me automate some tasks and to learn some Ruby along the way. I've hit a bump now. How can I open a file for appending data to it on a certain line number.
Examlpe:
File:
jon
mike
me
you
I want to open the above file and add a name on line 4 (between "me" and "you") to get:
File:
jon
mike
me
NEWNAME
you
I know the exact line number I want to append data to, while pushing existing data down.
I was thinking of loading the file into an array line by line, then adding to the array and finally saving it back again, but this is ugly and I believe there is an easier way.
Thanks for help,
-NSKL
|