Created
May 22, 2011 05:15
-
-
Save rochefort/985199 to your computer and use it in GitHub Desktop.
The array of the relationship between a child and his parents is output by the hierarchical display.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class Tree | |
def initialize | |
@data = Array.new | |
end | |
def <<(ary) | |
@data << ary | |
end | |
def to_s | |
root_nodes = @data.find_all { |elm| elm[1] == nil } | |
to_s_children(root_nodes) | |
end | |
private | |
def to_s_children(nodes, depth=0, indent=' ') | |
return nodes if nodes.size.zero? | |
nodes.sort_by{|x| x[0].to_s}.each do |node| | |
puts "#{indent*depth}#{node[0]}" | |
children = @data.find_all { |elm| elm[1] == node[0] } | |
to_s_children(children, depth+1) | |
end | |
end | |
end | |
if __FILE__ == $0 | |
tree = Tree.new | |
Module.constants.each do |elm| | |
const = RUBY_VERSION < "1.9" ? eval(elm) : eval(elm.to_s) | |
tree << [const, const.superclass] if const.respond_to? :superclass | |
end | |
puts tree | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment