version 0.1 - Aug.24.2000
version 0.2 - Aug.25.2000
require 'tree'
tree = Tree.new
tree << [1, 2, 3, 4]
tree << [2, 5, 6]
tree << [3, 7, 8]
print tree
print "parents of 1 are ", tree.parents(1).join(", "), "\n"
print "parents of 2 are ", tree.parents(2).join(", "), "\n"
print "children of 2 are ", tree.children(2).join(", "), "\n"
print "children of 8 are ", tree.children(8).join(", "), "\n"
print "3 is ", tree.relationship(3, 5), " of 5\n"
print "2 is ", tree.relationship(2, 8), " of 8\n"
print "8 is ", tree.relationship(8, 2), " of 2\n"
print "4 is ", tree.relationship(4, 6), " of 6\n"
--- 結果
+- 1
+- 2
+- 3
+- 4
+- 7
+- 8
+- 5
+- 6
parents of 1 are
parents of 2 are 1
children of 2 are 3, 5
children of 8 are
3 is brother or sister of 5
2 is ancestor of 8
8 is descendant of 2
4 is unknown relation of 6
print tree
---
tree.rbもrubyのメソッドツリーの使用例になっています。
if __FILE__ == $0
tree = Tree.new
Module.constants.sort.each do |x|
eval <<-EOT
if #{x}.is_a?(Class) then
tree << #{x}.ancestors.reverse
end
EOT
end
print tree.to_s(3)
print tree
p tree.sibling(Array)
tree.sibling(Array).each do |x|
p x
end
end
download