A Brief List of Less Used Libraries

Abbrev

Calculates the set of unambiguous abbreviations for a given set of strings.

require'abbrev'require'pp'ppAbbrev.abbrev(['ruby'])#=> {"ruby"=>"ruby", "rub"=>"ruby", "ru"=>"ruby", "r"=>"ruby"}ppAbbrev.abbrev(%w{ ruby rules })

Generates:

{"ruby"=>"ruby","rub"=>"ruby","rules"=>"rules","rule"=>"rules","rul"=>"rules"}

It also provides an array core extension, Array#abbrev.

pp%w{ summer winter }.abbrev

Generates:

{"summer"=>"summer","summe"=>"summer","summ"=>"summer","sum"=>"summer","su"=>"summer","s"=>"summer","winter"=>"winter","winte"=>"winter","wint"=>"winter","win"=>"winter","wi"=>"winter","w"=>"winter"}

Abbrev Reference

GetoptLong

The GetoptLong class allows you to parse command line options similarly to the GNU getopt_long() C library call. Note, however, that GetoptLong is a pure Ruby implementation.

GetoptLong allows for POSIX-style options like --file as well as single letter options like -f

The empty option -- (two minus symbols) is used to end option processing. This can be particularly important if options have optional arguments.

Here is a simple example of usage:

require'getoptlong'opts=GetoptLong.new(['--help','-h',GetoptLong::NO_ARGUMENT],['--repeat','-n',GetoptLong::REQUIRED_ARGUMENT],['--name',GetoptLong::OPTIONAL_ARGUMENT])dir=nilname=nilrepetitions=1opts.eachdo|opt,arg|caseoptwhen'--help'puts<<-EOF hello [OPTION] ... DIR -h, --help: show help --repeat x, -n x: repeat x times --name [name]: greet user by name, if name not supplied default is John DIR: The directory in which to issue the greeting.  EOFwhen'--repeat'repetitions=arg.to_iwhen'--name'ifarg==''name='John'elsename=argendendendifARGV.length!=1puts"Missing dir argument (try --help)"exit0enddir=ARGV.shiftDir.chdir(dir)foriin(1..repetitions)print"Hello"ifnameprint", #{name}"endputsend

Example command line:

hello -n 6 --name -- /tmp 

GetoptLong Reference

NKF

NKF - Ruby extension for Network Kanji Filter

NKF Reference

PStore

PStore implements a file based persistence mechanism based on a Hash. User code can store hierarchies of Ruby objects (values) into the data store file by name (keys). An object hierarchy may be just a single object. User code may later read values back from the data store or even update data, as needed.

The transactional behavior ensures that any changes succeed or fail together. This can be used to ensure that the data store is not left in a transitory state, where some values were updated but others were not.

Behind the scenes, Ruby objects are stored to the data store file with Marshal. That carries the usual limitations. Proc objects cannot be marshalled, for example.

PStore Reference

Rinda

A module to implement the Linda distributed computing paradigm in Ruby.

Rinda is part of DRb (dRuby).

Rinda Reference

TSort

TSort implements topological sorting using Tarjan’s algorithm for strongly connected components.

TSort is designed to be able to be used with any object which can be interpreted as a directed graph.

TSort requires two methods to interpret an object as a graph, tsort_each_node and tsort_each_child.

  • tsort_each_node is used to iterate for all nodes over a graph.
  • tsort_each_child is used to iterate for child nodes of a given node.

The equality of nodes are defined by eql? and hash since TSort uses Hash internally.

TSort Reference

WeakRef

Weak Reference class that allows a referenced object to be garbage-collected.

A WeakRef may be used exactly like the object it references.

Usage:

foo=Object.new# create a new object instancepfoo.to_s# original's classfoo=WeakRef.new(foo)# reassign foo with WeakRef instancepfoo.to_s# should be same classGC.start# start the garbage collectorpfoo.to_s# should raise exception (recycled)

WeakRef Reference

close