Class: Dictionary::WordPath
- Inherits:
-
Object
- Object
- Dictionary::WordPath
- Defined in:
- lib/ruby-dictionary/word_path.rb
Instance Method Summary collapse
- #<<(word) ⇒ Object
- #==(obj) ⇒ Object
- #case_sensitive? ⇒ Boolean
- #find(word) ⇒ Object
- #find_prefixes(string) ⇒ Object
- #hash ⇒ Object
-
#initialize(case_sensitive) ⇒ WordPath
constructor
A new instance of WordPath.
- #inspect ⇒ Object
- #leaf=(is_leaf) ⇒ Object
- #leaf? ⇒ Boolean
- #suffixes ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(case_sensitive) ⇒ WordPath
Returns a new instance of WordPath.
3 4 5 6 7 |
# File 'lib/ruby-dictionary/word_path.rb', line 3 def initialize(case_sensitive) @case_sensitive = !!case_sensitive @is_leaf = false @word_paths = {} end |
Instance Method Details
#<<(word) ⇒ Object
21 22 23 24 25 |
# File 'lib/ruby-dictionary/word_path.rb', line 21 def <<(word) raise ArgumentError, 'must be a string' unless word.kind_of?(String) word = word.downcase unless @case_sensitive _append(word.strip) end |
#==(obj) ⇒ Object
53 54 55 |
# File 'lib/ruby-dictionary/word_path.rb', line 53 def ==(obj) obj.class == self.class && obj.hash == self.hash end |
#case_sensitive? ⇒ Boolean
9 10 11 |
# File 'lib/ruby-dictionary/word_path.rb', line 9 def case_sensitive? @case_sensitive end |
#find(word) ⇒ Object
27 28 29 30 31 |
# File 'lib/ruby-dictionary/word_path.rb', line 27 def find(word) raise ArgumentError, 'must be a string' unless word.kind_of?(String) word = word.downcase unless @case_sensitive _find(word.strip) end |
#find_prefixes(string) ⇒ Object
42 43 44 45 46 |
# File 'lib/ruby-dictionary/word_path.rb', line 42 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 |
#hash ⇒ Object
49 50 51 |
# File 'lib/ruby-dictionary/word_path.rb', line 49 def hash self.class.hash ^ @is_leaf.hash ^ @word_paths.hash ^ @case_sensitive.hash end |
#inspect ⇒ Object
57 58 59 |
# File 'lib/ruby-dictionary/word_path.rb', line 57 def inspect "#<WordPath @is_leaf=#@is_leaf @word_paths={#{@word_paths.keys.join(',')}}>" end |
#leaf=(is_leaf) ⇒ Object
17 18 19 |
# File 'lib/ruby-dictionary/word_path.rb', line 17 def leaf=(is_leaf) @is_leaf = !!is_leaf end |
#leaf? ⇒ Boolean
13 14 15 |
# File 'lib/ruby-dictionary/word_path.rb', line 13 def leaf? @is_leaf end |
#suffixes ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/ruby-dictionary/word_path.rb', line 33 def suffixes [].tap do |suffixes| @word_paths.each do |letter, path| suffixes << letter if path.leaf? suffixes.concat(path.suffixes.collect { |suffix| "#{letter}#{suffix}" }) end end end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/ruby-dictionary/word_path.rb', line 61 def to_s inspect end |