May 14, 2018 Tags: crystal, devblog, programming
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.
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
:
1
2
3
4
5
6
7
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:
1
2
3
require "abbrev"
Abbrev.abbrev(%w{crystal rules}, "rul")
becomes this in Crystal with abbrev.cr:
1
2
3
require "abbrev"
Abbrev.abbrev(%w{crystal rules}, /\Arul/)
Thanks for reading!