Class | Elif |
In: |
lib/elif.rb
|
Parent: | Object |
A File-like object for reading lines from a disk file in reverse order. See Elif::new and Elif#gets for details. All other methods are just interface conveniences.
Based on Perl‘s File::ReadBackwards module, by Uri Guttman.
VERSION | = | "0.1.0".freeze | The version of the installed library. | |
MAX_READ_SIZE | = | 1 << 10 | The size of the reads we will use to add to the line buffer. |
The first half of the Elif algorithm (to read file lines in reverse order). This creates a new Elif object, shifts the read pointer to the end of the file, and prepares a buffer to hold read lines until they can be returned. This method also sets the @read_size to the remainer of File#size and MAX_READ_SIZE for the first read.
Technically args are delegated straight to File#new, but you must open the File object for reading for it to work with this algorithm.
The second half on the Elif algorthim (see Elif::new). This method returns the next line of the File, working from the end to the beginning in reverse line order.
It works by moving the file pointer backwords MAX_READ_SIZE at a time, storing seen lines in @line_buffer. Once the buffer contains at least two lines (ensuring we have seen on full line) or the file pointer reaches the head of the File, the last line from the buffer is returned. When the buffer is exhausted, this will throw nil (from the empty Array).