Skip to content

Instantly share code, notes, and snippets.

@dkan
Created June 15, 2012 00:55
Show Gist options
  • Save dkan/2933971 to your computer and use it in GitHub Desktop.
Save dkan/2933971 to your computer and use it in GitHub Desktop.
class Dictionary
def initialize
@dict = {}
end
def entries
@dict
end
def add(word)
# word_definition = {"word_definition" => nil} if word_definition.class == "String"
word_hash = word.class == "Hash" ? word : {word => nil}
@dict.merge!(word_hash)
end
def include?(word)
if @dict[word]
true
else
false
end
end
def empty?
end
def find(prefix)
found_words = {}
@dict.each do |word, definition|
if word.start_with? prefix
found_words.merge!({ word => definition })
end
end
found_words
end
def keywords
@dict.keys.sort
end
end
my_dictionary = Dictionary.new
my_dictionary.add("fish", "a swimming animal")
my_dictionary.add("fiend")
my_dictionary.add("apple")
puts my_dictionary.entries
puts
puts my_dictionary.keywords
puts
my_dictionary.find('nothing')
my_dictionary.find('fi')
class Book
EXCEPTIONS = [ 'a', 'an', 'the', 'and' ]
def initialize
@title = ""
end
def title=(words)
words.capitalize.split.map! do |word|
word.capitalize unless @exceptions.include?(word)
end
words.join(" ")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment