This gem implements a rooted, ordered tree, with a focus on easy iteration over nodes and access to basic tree properties.

gem install rooted_tree -v 0.3.4


🌳 RootedTree

Gem Version Build Status Inline docs Documentation

This gem implements a rooted, ordered tree, but that name is a bit of a mouthful. It is ment to be used as a building block when working with any tree shaped data. For a brief recap of the terminology please see below. Please refer to for a more in depth description.

       A     A is the root.
  ┌────┼──┐  B, C and D are all children of A.
  B    C  D  E is a descendant of A.
┌─┼─┐ ┌┴┐ │  A is of degree 3 while C is of degree 2.
E F G H I J  F is a leaf.


Add this line to your application's Gemfile:

gem 'rooted_tree'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rooted_tree


Please see the documentation for the complete API.

# Create some nodes
root =
child_a =
child_b =

# Put the two children below the root
root << child_a << child_b

# Look at the result
p root # => RootedTree::Node:0x3fd5d54efda0
       #    ├─╴RootedTree::Node:0x3fd5d54c3ea8
       #    └─╴RootedTree::Node:0x3fd5d54ba894

The gem is primarily ment to be extended by other classes. The following example builds a tree of the files in the file system and displays it much like the command line tool tree.

class FileSystemItem < RootedTree::Node
  def display
    inspect { |item| item.value }

  def self.map_to_path path = '.', root: new(path)
    # Iterate over all of the files in the directory
    Dir[path + '/*'].each do |entry|
      # Create a new FileSystemItem for the entry
      item = new File.basename(entry)
      root << item
      # Continue to map the files and directories under
      # entry, if it is a directory
      map_to_path entry, root: item unless File.file? entry


puts FileSystemItem.map_to_path('.').display


After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at[USERNAME]/rooted_tree.


The gem is available as open source under the terms of the MIT License.