Created
June 14, 2012 23:16
-
-
Save dkan/2933607 to your computer and use it in GitHub Desktop.
dictionary
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 | |
def initialize | |
@dict = {} | |
end | |
def entries | |
@dict | |
end | |
def add(word, definition = nil) | |
@dict[word] = definition | |
end | |
def include?(word) | |
if @dict[word] | |
true | |
else | |
false | |
end | |
end | |
def empty? | |
end | |
def find(prefix) | |
@dict.each do |word, definition| | |
if (word.include? prefix) == false | |
return nil | |
else | |
puts word + ": " + @dict[word] | |
end | |
end | |
end | |
def keywords | |
@dict.keys.sort | |
end | |
end | |
my_dictionary = Dictionary.new | |
my_dictionary.add("fish", "a swimming animal") | |
my_dictionary.add("fiend", "a bad person") | |
my_dictionary.add("apple") | |
puts my_dictionary.entries | |
puts | |
puts my_dictionary.keywords | |
puts | |
my_dictionary.find('nothing') | |
my_dictionary.find('fi') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment