forked from CoderAcademy-BRI/ruby-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_definition.rb
More file actions
21 lines (21 loc) · 861 Bytes
/
Copy path06_definition.rb
File metadata and controls
21 lines (21 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Definition
# This class stores a collection of definitions as an array of hashes. It's basically a dictionary.
def initialize
@entries = []
end
def add_word(word, definition) # Stores an entry in this object. A less-than-informative name is used for this method so that this program passes the bundled tests.
@entries << {:word => word, :definition => definition}
end
def total_words # Returns the number of entries in this object
return @entries.length
end
def lookup(word) # Searches every entry for a word and returns its definition. If no match is found, returns nil.
definition = nil
for entry in @entries do
if entry[:word] == word
definition = entry[:definition]
end
end
return definition
end
end