Tree traversal in Ruby, a response to http://michaelfeathers.typepad.com/michael_feathers_blog/2013/02/sub-tree-selection-with-flattenselect.html
✓ Published0🌍 Public
FFranklin Chen
Last edited Feb 15, 2013
Created on Feb 15, 2013
This example demonstrates a preorder tree-traversal algorithm in Ruby, using the `TreeEnumerable` class and the `Enumerable` mixin to flatten a nested abstract syntax tree (AST) into a list of matching subtrees. The `sexp_select` method filters nodes by their symbol type, such as `:@ivar`, returning only those subtrees. The code relies on Ruby’s `find_all` from `Enumerable` and a custom `tree_traverse` that yields each array node recursively. The example includes a MiniTest unit test (`TestTreeSelect`) that asserts the filtered result from a hardcoded parse-tree structure, showing how the selection works on real S-expression data.
AI-generated description