Created
June 14, 2012 20:48
-
-
Save perspectivezoom/2932848 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
attr_accessor :entries, :keywords | |
def initialize() | |
@entries = {} | |
@keywords = [] | |
end | |
def add(input) | |
if input.is_a?(String) | |
str = input | |
@entries.merge!({str => nil}) | |
@keywords << str | |
elsif input.is_a?(Hash) | |
hsh = input | |
@entries.merge! hsh | |
@keywords += hsh.keys | |
else | |
raise "Not given a String or Hash" | |
end | |
@keywords.sort! | |
end | |
def include?(str) | |
@keywords.include? str | |
end | |
def find(str) | |
matches = {} | |
@entries.each_with_index do |(key,value), index| | |
if key.start_with?(str) | |
matches.merge!({key => value}) | |
end | |
end | |
matches | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment