Calculate the set of unique abbreviations for a given set of strings.
[["rub", "ruby"], ["ruby", "ruby"], ["rul", "rules"], ["rule", "rules"], ["rules", "rules"]]
This can be quite useful, for example when writing a command line script which takes a number of options.
Let’s say you want to make a script that can make the basic CRUD operations. So want to be able to call it like this from the command line:
> my_script create > my_script delete
The following script allows you to use any abbreviated command as long as it is unambiguous.
# my_script.rb require 'abbrev' command = ARGV.first actions = %w[create read update delete] mappings = Abbrev::abbrev(actions) puts mappings[command]
That means you can call it like this:
> my_script cr > my_script d
And it will print: