lib/ruby-dictionary/word_path.rb in ruby-dictionary-1.1.0 vs lib/ruby-dictionary/word_path.rb in ruby-dictionary-1.1.1

- old
+ new

@@ -37,10 +37,17 @@ suffixes.concat(path.suffixes.collect { |suffix| "#{letter}#{suffix}" }) end end end + def find_prefixes(string) + raise ArgumentError, 'must be a string' unless string.kind_of?(String) + string = string.downcase unless @case_sensitive + _find_prefixes(string.strip) + end + + def hash self.class.hash ^ @is_leaf.hash ^ @word_paths.hash ^ @case_sensitive.hash end def ==(obj) @@ -63,9 +70,21 @@ if word.size == 1 word_path else word_path._find(word[1, word.size]) + end + end + + def _find_prefixes(str) + char = str[0] + + word_path = @word_paths[char] + return [] unless word_path + + [].tap do |prefixes| + prefixes << char if word_path.leaf? + prefixes.concat(word_path._find_prefixes(str[1, str.size]).collect { |prefix| "#{char}#{prefix}" }) end end def _append(word) return if word.empty?