Skip to content

Instantly share code, notes, and snippets.

@basicxman
Created August 14, 2011 04:08
Show Gist options
  • Select an option

  • Save basicxman/1144561 to your computer and use it in GitHub Desktop.

Select an option

Save basicxman/1144561 to your computer and use it in GitHub Desktop.
def resolve_key(key)
bucket = key[0]
return key if @item_buckets[bucket].include? key
shortest_diff = nil
shortest_key = nil
@item_buckets[bucket].each do |test_key|
if test_key.length > key.length
diff = test_key.length - key.length if test_key.index(key)
else
diff = key.length - test_key.length if key.index(test_key)
end
next if diff.nil?
if shortest_diff.nil? or diff < shortest_diff
shortest_key = key
shortest_diff = diff
end
end
return shortest_key
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment