Class: Dictionary::WordPath

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-dictionary/word_path.rb

Instance Method Summary collapse

Constructor Details

#initializeWordPath

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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

#hashObject



35
36
37
# File 'lib/ruby-dictionary/word_path.rb', line 35

def hash
  self.class.hash ^ @is_leaf.hash ^ @word_paths.hash
end

#inspectObject



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

Returns:

  • (Boolean)


8
9
10
# File 'lib/ruby-dictionary/word_path.rb', line 8

def leaf?
  @is_leaf
end

#suffixesObject



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_sObject



47
48
49
# File 'lib/ruby-dictionary/word_path.rb', line 47

def to_s
  inspect
end