Skip to content

Instantly share code, notes, and snippets.

@ryanlecompte
Created June 5, 2011 13:43
Show Gist options
  • Save ryanlecompte/1008972 to your computer and use it in GitHub Desktop.
Save ryanlecompte/1008972 to your computer and use it in GitHub Desktop.
nested dictionary searching
dict = {}
'aardvark'.chars.inject(dict) { |dict,c| dict[c] = {} }
def find(key, dict)
current_dict = dict
key.chars.each do |c|
return nil unless current_dict.has_key?(c)
current_dict = current_dict[c]
end
end
puts 'Found key' if find('aardvark', dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment