I’d like to write a Ruby script that would copy specific lines of code from one file and insert them into another file. I’d like, if possible, the result to be the same as if I had highlighted some lines in file_1, copied them, then placed my cursor in the middle of file_2 and pasted the copied code in (ie. code in file_2 that’s beneath the insertion point would move down…).
I have written scripts in the past that take the entire contents of a file and insert it into an empty file, but I’d like just certain lines from one file to be “copied and pasted” into the middle of another file. Is it possible to search a file by line number? Is my overall goal possible? Thanks in advance.
Try each_with_index, if you can keep the whole file in memory the script is pretty straightforward.
You can then redirect to a new file or open up a new one and write to that using
If you have the list of line numbers you could just walk through those too: