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);
}
Published 3 Mar 2013

 

Sorting a Ruby hash by key or value.

by key

h.sort

by value

h.sort_by {|k,v| v}

Note both forms return an array of key-value pairs (i.e., an array of arrays).

Tagged ruby and dev.

 

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 (and array[-1]) operates like array.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 (and array[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
Tagged ruby and dev.

 

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 }
Tagged ruby, dev and cli.

 

Split a Ruby array into two halves.

To split a Ruby array into two equally-sized (+/-1) parts:

left,right = a.each_slice( (a.size/2.0).round ).to_a

For example:

a = [1,2,3,4,5]                         # => [1, 2, 3, 4, 5]
a.each_slice( (a.size/2.0).round ).to_a # => [[1, 2, 3], [4, 5]]
Tagged ruby and dev.

 

Split a Ruby array into N equally-sized parts.

To split a Ruby array into n equally-sized parts:

a.each_slice( (a.size/n.to_f).round ).to_a

For example:

a = [1,2,3,4,5]; n = 3                     # => 3
a.each_slice( (a.size/n.to_f).round ).to_a # => [[1, 2], [3, 4], [5]]
Tagged ruby and dev.

 

This page was generated at 4:16 PM on 26 Feb 2018.
Copyright © 1999 - 2018 Rodney Waldhoff.