Skip to content

Instantly share code, notes, and snippets.

@dmerrick
Created July 22, 2009 20:24
Show Gist options
  • Save dmerrick/152250 to your computer and use it in GitHub Desktop.
Save dmerrick/152250 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -wKU
# TODO: use the awesome Array#abbrev method
#require 'abbrev' if RUBY_VERSION.to_f <= 1.9
hosts = ["fubar", "foobar"] # hosts you're setting mappings to
aliases = [] # datastructure to hold mappings
# build the datastructure
# starting with "fu" => "fubar", "fub" => "fubar", etc.
hosts.each do |host|
a = []
1.upto(host.size) {|n| a << host[0..n] }
aliases << [a, host]
end
# add custom aliases
to_add = "baz"
location = "fubar"
aliases.map {|alia, result| alia << to_add if result == location }
# aliases now looks like:
# [[["fu", "fub", "fuba", "fubar", "fubar", "baz"], "fubar"],
# [["fo", "foo", "foob", "fooba", "foobar", "foobar"], "foobar"]]
# lookup
input = "foo"
aliases.each {|alia, result| puts result if alia.include?(input)}
# => "foobar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment