Created
July 22, 2009 20:24
-
-
Save dmerrick/152250 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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