Skip to content

Latest commit

 

History

History
68 lines (49 loc) · 1.79 KB

README.markdown

File metadata and controls

68 lines (49 loc) · 1.79 KB

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.

Advantages

Although it acts like a stream, ARGFy keeps more information about the current input file. These internal attributes are:

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

Documentation

There is RDoc Documentation.

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. Here is the included sample.rb script:

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
puts

An example usage on the included test files is:

shell> ruby sample.rb test/in1.txt test/in2.txt

test/in1.txt
------------
  1: one
  2: two

test/in2.txt
------------
  1: alpha, beta, gamma
  2: 0987654321
  3: 
  4: NOT BLANK!

Contact

  • Author: Joseph Pecoraro (email)
  • Website: http://blog.bogojoker.com
  • Copyright: Copyright (c) 2008 Joseph Pecoraro
  • License: Distributes under the same terms as Ruby