Skip to content

Instantly share code, notes, and snippets.

Created July 20, 2012 18:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Lazy Levenshtein
# test by running:
# ruby lazy.rb
# use control+c to exit
require 'amatch'
include Amatch
def lazy input, matches, abbreviations=true
# setup the Levenshtein comparator
distances = {}
m =
matches.reverse.each do |match|
# get the edit distance
tests = []
tests << m.match(match)
tests << m.match(match[0..3]) if abbreviations
# lowest score gets placed
distances[tests.min] = match
# return input, returns original if matches is empty
input = distances.empty? ? input : distances.min.last
environments = %w(development integration staging production)
while true
puts "\nenvironments #{environments.join(', ')}"
print "choose environment: "
input = gets.chomp
puts "match: #{lazy(input, environments)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment