Programming, philosophy, pedaling.

Introducing abbrev.cr

May 14, 2018

Tags: crystal, programming, devblog

This is just a quick announcement for abbrev.cr, my latest Crystal library ("shard").

abbrev.cr is a direct port of Ruby's Abbrev module to Crystal. At its core, it's only about 30 lines.

Installation and usage:

The easiest way to install abbrev.cr is via Crystal's shards dependency manager. You can find the relevant steps in the repository README.

In use, abbrev.cr is almost identical to Ruby's Abbrev, supplying both Abbrev.abbrev and Array#abbrev:

require "abbrev"

Abbrev.abbrev(%{crystal}) # => {"crystal" => "crystal", "crysta" => "crystal", "cryst" => "crystal", "crys" => "crystal", "cry" => "crystal", "cr" => "crystal", "c" => "crystal"}

Abbrev.abbrev(%w{crystal ruby}, /^r/) # => {"ruby" => "ruby", "rub" => "ruby", "ru" => "ruby", "r" => "ruby"}

%w{crystal is fun}.abbrev # => {"crystal" => "crystal", "crysta" => "crystal", "cryst" => "crystal", "crys" => "crystal", "cry" => "crystal", "cr" => "crystal", "c" => "crystal", "is" => "is", "i" => "is", "fun" => "fun", "fu" => "fun", "f" => "fun"}

Unlike Ruby's Abbrev, abbrev.cr will not take a string for its (optional) pattern argument — the pattern must be a regular expression. As such, a Ruby invocation like this:

require "abbrev"

Abbrev.abbrev(%w{crystal rules}, "rul")

becomes this in Crystal with abbrev.cr:

require "abbrev"

Abbrev.abbrev(%w{crystal rules}, /\Arul/)

Thanks for reading!

Reddit discussion