Skip to content

Instantly share code, notes, and snippets.

@jackieiscool
Created June 16, 2012 01:05
Show Gist options
  • Save jackieiscool/2939446 to your computer and use it in GitHub Desktop.
Save jackieiscool/2939446 to your computer and use it in GitHub Desktop.
Creating a dictionary class
class Dictionary
attr_reader :entries
def initialize()
@entries = {}
end
def add(words)
if words.class == String
@entries.merge!({words => nil})
else
@entries.merge!(words)
end
end
def keywords
@entries.keys.sort
end
def include?(keyword)
@entries.include?(keyword)
end
def find(str)
#@entries.inject({}) do |found_words, (word, definition)|
# found_words.update word => definition if word[0,str.length] == str
#end
l = str.length
found_words = {}
@entries.each do |word, definition|
if word[0,l] == str
found_words.update word => definition
end
end
found_words
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment