Class ARGFy
In: ARGFy.rb
Parent: Object

ARGFy - a more flexible ARGF

This class is a more flexible version of the ARGF global. The constructor takes an array, a list of files, and processes them like one constant stream of input. Reading from the stream is always line by line either all at once using each or one at a time using gets.

Attributes

Internal states can help your program understand what was processed when you pull any line:

  • lineno - line number of the entire stream
  • filelineno - line number of the current file
  • filename - current file being processed

Example

This is a simple program that prints out each file with a simple header when the input files change. It shows the main advantage of this class over ARGF.

    require 'ARGFy'

    argf = ARGFy.new(ARGV)
    argf.each do |line|

      # Per File Header
      if argf.new_file?
        filename = argf.filename
        filename = "STDIN" if filename == "-"
        puts '', filename, "-"*filename.length
      end

      # Print out the line with line numbers
      puts "%3d: %s" % [argf.filelineno, line]

    end

Contact

Author:Joseph Pecoraro (joepeck02@gmail.com)
Copyright:Copyright © 2008 Joseph Pecoraro
License:Distributes under the same terms as Ruby

Methods

add_file   each   gets   new   new_file?   reset  

Included Modules

Enumerable

Attributes

filelineno  [RW]  last line number read from the current file
filename  [RW]  filename of the current file being read
lineno  [RW]  last line number read from the entire stream

Public Class methods

Constructor takes an optional list of files, and will default to just using STDIN on an empty list.

Public Instance methods

Add a file to the file list

Goes through the lines of all files constantly updating the internal states filename, lineno, and filelineno.

Gets each line individually.

Returns true if the last line read was from a new file

Sets the public and private states to their default values

[Validate]