Skip to content

Instantly share code, notes, and snippets.

@rochefort
Created May 22, 2011 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rochefort/985199 to your computer and use it in GitHub Desktop.
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.
#!/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