Masa331 blogging

my-photo

Hi, i'm Premysl Donat and this is my blog about IT and my life in generall. I work at UOL and in my free time.. well, look at my Github.

Have a good one!

Menu

Abbrev from ruby standard lib

Did you know about Ruby’s standard lib Abbrev? No? Well then let’s take a look!

What Abbrev does

For each string in an array Abbrev gives you all possible uniq abbreviations:

require 'abbrev'

['table', 'wall'].abbrev
=> {"table"=>"table", "tabl"=>"table", "tab"=>"table", "ta"=>"table", "t"=>"table", "wall"=>"wall", "wal"=>"wall", "wa"=>"wall", "w"=>"wall"}

The Implementation is really simple and gives you 2 possibilities how to get your abbreviations. Either by Abbrev::abbrev(words, pattern = nil) or by calling #abbrev directly on any Array.

What’s it good for?

I think CLI’s could use it. For example with Git’s cli you can just call shortest possible abbreviation instead of original full command. And it saves a lot of keystrokes.

I can’t find any other valid case yet. But for some reason i really like the library. So i was just playing a bit for now and created this gem AbbreviatedMethods. With it you can call all your’s objects methods by their’s shortest possible abbreviations:

I don’t think it’s really useful in production but definitelly interesting thing to play with. Have fun!

P.S.: If you find any use-case please let me know