BasicTree

A basic Ruby tree structure with nice syntax.

Example

fruit = BasicTree.new "Fruit" do
  add "Apple" do
    add "Red Delicious"
  end
  add "Banana" do
    add "Manzano"
    add "Plantain"
  end
  add "Orange"
end

fruit.object
# "Fruit"

banana = fruit.get(2) # get the 2nd fruit
banana.object # "Banana"

plantain = banana.get(2) # get the 2nd banana
plantain.object # "Plantain"

plantain.parent.object # "Banana"

plantain.path.map(&:object)
# ["Fruit", "Banana", "Plantain"]

plantain.ancestors.map(&:object) # ancestors is like path except it doesn't include itself
# ["Fruit", "Banana"]

banana.subtree.map(&:object)
# ["Banana", "Manzano", "Plantain"]

fruit.descendants.map(&:object) # descendants is like subtree except it doesn't include itself
# ["Apple", "Red Delicious", "Banana", "Manzano", "Plantain", "Orange"]

banana.siblings.map(&:object)
# ["Apple", "Orange"]

plantain.root.object # "Fruit"

plantain.level # 3

banana.root? # false

fruit.root? # true

banana.leaf? # false

plantain.leaf? # true

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

See LICENSE for details.