Class: Dictionary::WordPath
- Inherits:
-
Object
- Object
- Dictionary::WordPath
- Defined in:
- lib/ruby-dictionary/word_path.rb
Instance Method Summary collapse
- #<<(word) ⇒ Object
- #==(obj) ⇒ Object
- #find(word) ⇒ Object
- #hash ⇒ Object
-
#initialize ⇒ WordPath
constructor
A new instance of WordPath.
- #inspect ⇒ Object
- #leaf=(is_leaf) ⇒ Object
- #leaf? ⇒ Boolean
- #suffixes ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ WordPath
Returns a new instance of WordPath.
3 4 5 6 |
# File 'lib/ruby-dictionary/word_path.rb', line 3 def initialize @is_leaf = false @word_paths = {} end |
Instance Method Details
#<<(word) ⇒ Object
16 17 18 19 |
# File 'lib/ruby-dictionary/word_path.rb', line 16 def <<(word) raise ArgumentError, "must be a string" unless word.kind_of?(String) _append(word.strip.downcase) end |
#==(obj) ⇒ Object
39 40 41 |
# File 'lib/ruby-dictionary/word_path.rb', line 39 def ==(obj) obj.class == self.class && obj.hash == self.hash end |
#find(word) ⇒ Object
21 22 23 24 |
# File 'lib/ruby-dictionary/word_path.rb', line 21 def find(word) raise ArgumentError, "must be a string" unless word.kind_of?(String) _find(word.strip.downcase) end |
#hash ⇒ Object
35 36 37 |
# File 'lib/ruby-dictionary/word_path.rb', line 35 def hash self.class.hash ^ @is_leaf.hash ^ @word_paths.hash end |
#inspect ⇒ Object
43 44 45 |
# File 'lib/ruby-dictionary/word_path.rb', line 43 def inspect "#<WordPath @is_leaf=#@is_leaf @word_paths={#{@word_paths.keys.join(',')}}>" end |
#leaf=(is_leaf) ⇒ Object
12 13 14 |
# File 'lib/ruby-dictionary/word_path.rb', line 12 def leaf=(is_leaf) @is_leaf = !!is_leaf end |
#leaf? ⇒ Boolean
8 9 10 |
# File 'lib/ruby-dictionary/word_path.rb', line 8 def leaf? @is_leaf end |
#suffixes ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/ruby-dictionary/word_path.rb', line 26 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
47 48 49 |
# File 'lib/ruby-dictionary/word_path.rb', line 47 def to_s inspect end |