Skip to content

Instantly share code, notes, and snippets.

@mattgaidica mattgaidica/lazy.rb
Created Jul 20, 2012

Embed
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 = Levenshtein.new(input)
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
end
# return input, returns original if matches is empty
input = distances.empty? ? input : distances.min.last
end
environments = %w(development integration staging production)
while true
puts "\nenvironments #{environments.join(', ')}"
print "choose environment: "
input = gets.chomp
puts "match: #{lazy(input, environments)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.