Class: Decode::Trie::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/trie.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



26
27
28
29
# File 'lib/decode/trie.rb', line 26

def initialize
  @values = nil
  @children = Hash.new
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



32
33
34
# File 'lib/decode/trie.rb', line 32

def children
  @children
end

#valuesObject

Returns the value of attribute values.



31
32
33
# File 'lib/decode/trie.rb', line 31

def values
  @values
end

Instance Method Details

#lookup(path, index = 0) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/decode/trie.rb', line 34

def lookup(path, index = 0)
  if index < path.size
    if child = @children[path[index]]
      return child.lookup(path, index+1)
    end
  else
    return self
  end
end

#traverse(path = []) {|path, values| ... } ⇒ Object

Yields:



44
45
46
47
48
49
50
# File 'lib/decode/trie.rb', line 44

def traverse(path = [], &block)
  yield path, values if values
  
  @children.each do |name, node|
    node.traverse([*path, name], &block)
  end
end