Rediscovering Ruby: Abbrev

The Ruby API is full of hidden-in-plain-sight libraries that
you’ll probably find extremely useful if only you knew they were
there in the first place.For instance if you go to the Ruby Doc
Standard Library right at the top, the only entry under ‘A’
is Abbrev,
a one function API that might save some trouble when it comes to text
processing.

The single function of the Abbrev library is to “calculate the
set of unique abbreviations for a given set of strings”. Put
slightly more simply given an array of strings, Abbrev will return a
list of unique non-ambiguous prefixes for each string such that no two
strings have the same prefix. So given the words ‘fox’ and
‘fig’ the unique abbreviations for ‘fox’ are
‘fo’ and ‘fox’ and for ‘fig’ is
‘fi’ and ‘fig’ with the prefix of
‘f’ being ambiguous and therefore excluded.

Abbrev has one method abbrev that can either be called as a
module method on the Abbrev module or mixed in to
Array. This method will return a hash comprising of the
abbreviation as the key and the original word as the value.

The first usage that I had for Abbrev was to generate short codes to
use as labels given a list of names for a graph. Usually the labels all
begin with a unique letter but in the odd case where that is not the
case a two letter short label will suffice. First we get the list of
abbreviations:

In terms of visual appeal of abbreviated labels I’ve found
that sometimes it’s best to strip vowels out of words. For
instance for ‘Fox’ and ‘Fig’, ‘FX’
and ‘FG’ are much more readable than ‘FO’ and
‘FI’.

About Aimred

We provide Ruby and Ruby on Rails development, consulting and
training services to businesses and organisations of all sizes. If
you want to find out how we can help you, contact us at info@aimred.com.