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
.
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
There is RDoc Documentation.
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!
- Author: Joseph Pecoraro (email)
- Website: http://blog.bogojoker.com
- Copyright: Copyright (c) 2008 Joseph Pecoraro
- License: Distributes under the same terms as Ruby