Skip to content

Instantly share code, notes, and snippets.

@perspectivezoom
Created June 14, 2012 20:48
Show Gist options
  • Save perspectivezoom/2932848 to your computer and use it in GitHub Desktop.
Save perspectivezoom/2932848 to your computer and use it in GitHub Desktop.
class Dictionary
attr_accessor :entries, :keywords
def initialize()
@entries = {}
@keywords = []
end
def add(input)
if input.is_a?(String)
str = input
@entries.merge!({str => nil})
@keywords << str
elsif input.is_a?(Hash)
hsh = input
@entries.merge! hsh
@keywords += hsh.keys
else
raise "Not given a String or Hash"
end
@keywords.sort!
end
def include?(str)
@keywords.include? str
end
def find(str)
matches = {}
@entries.each_with_index do |(key,value), index|
if key.start_with?(str)
matches.merge!({key => value})
end
end
matches
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment