Skip to content

Instantly share code, notes, and snippets.

@dkan
Created June 14, 2012 23:16
Show Gist options
  • Save dkan/2933607 to your computer and use it in GitHub Desktop.
Save dkan/2933607 to your computer and use it in GitHub Desktop.
dictionary
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