Class: Dictionary::WordPath

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

Instance Method Summary collapse

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

Raises:

  • (ArgumentError)


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

Returns:

  • (Boolean)


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

def case_sensitive?
  @case_sensitive
end

#find(word) ⇒ Object

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

#hashObject



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

#inspectObject



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

Returns:

  • (Boolean)


13
14
15
# File 'lib/ruby-dictionary/word_path.rb', line 13

def leaf?
  @is_leaf
end

#suffixesObject



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_sObject



61
62
63
# File 'lib/ruby-dictionary/word_path.rb', line 61

def to_s
  inspect
end