Module: OpsWalrus::Traversable

Included in:
Bundler, LoadPath, RuntimeEnvironment
Defined in:
lib/opswalrus/traversable.rb

Instance Method Summary collapse

Instance Method Details

#pre_order_traverse(root, observed_nodes = Set.new, &visit_fn_block) ⇒ Object

the yield block visits the node and returns children that should be visited



4
5
6
7
8
9
10
11
12
13
# File 'lib/opswalrus/traversable.rb', line 4

def pre_order_traverse(root, observed_nodes = Set.new, &visit_fn_block)
  # there shouldn't be any cycles in a tree, but we're going to make sure!
  return if observed_nodes.include?(root)
  observed_nodes << root

  children = visit_fn_block.call(root)
  children&.each do |child_node|
    pre_order_traverse(child_node, observed_nodes, &visit_fn_block)
  end
end