Hi just getting into Ruby, and I am trying to learn some basic file reading commands, and I haven’t found any solid sources yet.
I am trying to go through certain lines from that file, til the end of the file.
So in the file where it says FILE_SOURCES I want to read all the sources til end of file, and place them in a file.
I found printing the whole file, and replacing words in the file, but I just want to read certain parts in the file.
I would do it like this (assuming you can read the entire file into memory):
Relevant methods:
IO.readlinesto read the lines into an arrayArray#indexto find the index of the first line matching a regular expressionFile.opento create a new file on disk (and automatically close it when done)Array#[]to get the subset of lines from the index to the endIf you can’t read the entire file into memory, then I’d do a simpler variation on @tadman’s state-based one:
Welcome to Ruby!