Project

tree_node

0.0
No commit activity in last 3 years
No release in over 3 years
Non intrusive simple node implementation
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.8.3
>= 0
~> 3.12
 Project Readme

tree_node¶ ↑

Simple tree node implementation

Usage¶ ↑

require 'rubygems'
require 'tree_node'

class Link
  include Tree::Node
  attr_accessor :name, :id
  def initialize(name)
    @name     = "#{name}"
    @id       = Time.now.to_f * 1.0
  end
end

grampa          = Link.new(:grampa)
father          = Link.new(:father)
uncle           = Link.new(:uncle)
first_son       = Link.new(:first_son)
second_son      = Link.new(:second_son)
daughter        = Link.new(:daughter)
first_cousin    = Link.new(:first_cousin)
second_cousin   = Link.new(:second_cousin)
great_grand_son = Link.new(:great_grand_son)

grampa.add_child(father)
  father.add_child(first_son)
  father.add_child(second_son)
  father.add_child(daughter)
grampa.add_child(uncle)
  uncle.add_child(first_cousin)
  uncle.add_child(second_cousin)
    second_cousin.add_child(great_grand_son)

puts great_grand_son.root { |node| node.name }
puts great_grand_son.map(:up) { |node| node.name }.inspect
puts grampa.map(:down) { |node| node.name }.inspect

Output¶ ↑

grampa
["grampa", "uncle", "second_cousin", "great_grand_son"]
["grampa", "father", "first_son", "second_son", "daughter", "uncle", "first_cousin", "second_cousin", "great_grand_son"]

Copyright © 2012 kazuyoshi tlacaelel. See LICENSE.txt for further details.