Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Created January 25, 2013 07:20
Show Gist options
  • Save mfpiccolo/4632464 to your computer and use it in GitHub Desktop.
Save mfpiccolo/4632464 to your computer and use it in GitHub Desktop.
Create a new Dictionary with Dictionary.new. Then you can fill it with words and their corresponding definitions with the .new_words method. Put them in alphabetical order with .alpha and search the dictionary with .search. Create a new dictionary with .new_hash method.
class Dictionary
attr_accessor :new_hash
def initialize
empty_hash = Hash.new
@empty_hash = empty_hash
end
def new_words(new_hash)
dictionary1 = @empty_hash.merge(new_hash)
@dictionary1 = dictionary1
end
def alpha
@dictionary1 = Hash[@dictionary1.sort]
end
def search(letters)
@dictionary1.select {|word, defonition| word.include?(letters)}
end
end
my_ditionary = Dictionary.new
puts "'#{my_ditionary.new_words({'cobalt'=>1,'adanine'=>2,'dimene'=>3,'boron'=>4})} should equal {'c'=>1,'a'=>2,'d'=>3,'b'=>4}"
puts "'#{my_ditionary.alpha} should equal {'c'=>1,'a'=>2,'d'=>3,'b'=>4}"
puts "'#{my_ditionary.search("co")} should equal {'cobalt'=>1}"
puts my_ditionary.new_hash=({1=>"a",2=>"b"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment