Skip to content

Instantly share code, notes, and snippets.

@rtoal
Created July 17, 2013 03:06
Show Gist options
  • Save rtoal/6017359 to your computer and use it in GitHub Desktop.
Save rtoal/6017359 to your computer and use it in GitHub Desktop.
Generates the tree of Ruby core classes that aren't themselves in any modules
# Writes the class hierarchy of the core Ruby classes.
$nodes = Hash.new
module Hack # Hack to keep Node out of the result!
class Node
attr_reader :cls
attr_accessor :children
def initialize(c)
@cls = c
@children = []
end
end
end
def node_for(c)
$nodes[c] = Hack::Node.new c if not $nodes.has_key?(c)
$nodes[c]
end
def modules_in(c)
result = c.included_modules
(c.ancestors - [c]).each {|a| result -= a.included_modules}
result
end
def pretty_list(a)
a.empty? ? "" : "(#{a.join(',')})"
end
def print_tree(n, indent = "")
puts("#{indent}#{n.cls.name} #{pretty_list(modules_in(n.cls))}")
n.children.each do |child|
print_tree(child, indent + " ")
end
end
Module.constants.each do |constant|
c = Kernel.const_get(constant)
if Class === c
n = node_for c
if c.superclass
s = node_for(c.superclass)
s.children << n
end
end
end
print_tree(node_for(BasicObject))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment