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.

Methods

close   each   each_line   foreach   gets   new   open   readline   readlines   readlines  

Included Modules

Enumerable

Constants

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.

Public Class methods

Works just line File::foreach, save that the lines come in reverse order.

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.

Works just line File::open.

Works just line File::readlines, save that line Array will be in reverse order.

Public Instance methods

Works just line File#close.

Works just line File#each, save that the lines come in reverse order.

each_line(sep_string = $/)

Alias for each

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).

Works just line File#readline, save that the lines come in reverse order.

Works just line File#readlines, save that line Array will be in reverse order.

[Validate]