Created
June 16, 2012 01:05
-
-
Save jackieiscool/2939446 to your computer and use it in GitHub Desktop.
Creating a dictionary class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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