Sha256: 856168c7ea642e558fc722c11d5b5df3e51e0a70ef09ecef8080f7d38fae19c5
Contents?: true
Size: 1.06 KB
Versions: 6
Compression:
Stored size: 1.06 KB
Contents
module Flutterby # A helper module with methods to walk across a node tree in various # directions and variations and perform a block of code on each passed node. # module TreeWalker extend self # Walk the tree up, invoking the passed block for every node # found on the way, passing the node as its only argument. # def walk_up(node, val = nil, &blk) val = blk.call(node, val) node.parent ? walk_up(node.parent, val, &blk) : val end # Walk the graph from the root to the specified node. Just like {#walk_up}, # except the block will be called on higher level nodes first. # def walk_down(node, val = nil, &blk) val = node.parent ? walk_up(node.parent, val, &blk) : val blk.call(node, val) end # Walk the entire tree, top to bottom, starting with its root, and then # descending into its child layers. # def walk_tree(node, val = nil, &blk) val = blk.call(node, val) node.children.each do |child| val = walk_tree(child, val, &blk) end val end end end
Version data entries
6 entries across 6 versions & 1 rubygems