6 ruby snippets
Ruby-like ARGF for Node.js
tokuhirom's node-argf module offers a Ruby-like ARGF for Node.js.
Install via:
npm install argf
or by adding
{
"dependencies" : {
"argf" : "latest"
}
}
to your package.json
file.
Use ARGF like this:
ARGF = require('argf');
argf = new ARGF(); // create argf based on current
// command line parameters or
// input streams.
// register a callback for when all input data has been read
argf.on('finished', function() {
console.log("Done processing all inputs.");
});
// process the input(s)
argf.forEach( function(line) {
console.log("Read:",line);
console.log("From source:",argv.stream.path);
}
Like Ruby's ARGF
, the module assumes any elements in process.argv
represent files to process (and uses the input stream if no files are provided.
You can also pass an array to new ARGF()
to provide the list of files, which is handy if you're using something like node-optimist. (Note that in node-optimist you can use argv._
to get the remaining parameters after parsing.) For example:
optimist = require('optimist');
ARGF = require('argf');
options = {
# ...
}
argv = optimist.usage('Usage: $0 ...', options).argv;
argf = new ARGF(argv._);
argf.on('finished', function() {
console.log("Done processing all inputs.");
});
// process the input(s)
argf.forEach( function(line) {
console.log("Read:",line);
console.log("From source:",argv.stream.path);
}
Using Ruby arrays as stacks and queues.
array.push
appends an element to the array.array.pop
removes (and returns) the last element in the array.- Hence
array.last
(andarray[-1]
) operates likearray.peek
would if it existed--it returns (but does not remove) the item on the top of the stack. array.shift
removes (and returns) the first element in the array.- Hence
array.shift
"pops" an element in a queue-like way--first in, first out.array.first
(andarray[1]
) allow one to "peek" at this element.
> a = [ 1, 2, 3 ] # => [1, 2, 3]
> a.push 4 # => [1, 2, 3, 4]
> a.pop # => 4
> a # => [1, 2, 3]
> a.last # => 3
> a # => [1, 2, 3]
> a.shift # => 1
> a # => [2, 3]
> a.first # => 2
Reading from input files or STDIN in Ruby using ARGF.
ARGF
makes it easy for a Ruby script to read from STDIN, a file specified on the command-line argument or multiple files specified on the command line, all through the same interface.
Recall that ARGV
array contains the arguments passed to your Ruby script on the command line.
ARGF
assumes that any elements that remain in ARGV
represent files. Methods like ARGF.each
(accepting a block) and ARGF.readlines
(returning an array) operate on the concatenation of all files found in ARGV
. If ARGV
is empty, then ARGF
operates on STDIN instead.
For example, a cat
-like program could be implemented in Ruby as:
ARGF.each_line { |line| puts line }
When working with optparse
, use the parse!
method to strip recognized "flag" parameters from ARGV
, leaving only the files you want to operate so that ARGF
works just like you want it to. For example:
require 'optparse'
options = { }
opt_parser = OptionParser.new do |opt|
opt.banner = "Usage: #{$0} [OPTIONS]"
opt.separator ""
opt.separator "OPTIONS"
opt.on("-h","--heading HEADING","a heading to display.") do |heading|
options[:heading] = heading
end
opt.on("-v","--verbose","be more chatty") do
options[:verbose] = true
end
end
opt_parser.parse!
puts options[:heading] unless options[:heading].nil?
ARGF.each_line { |line| puts line }