Created
May 25, 2014 15:36
-
-
Save Quintus/d5c9ebb427fed600ba9c to your computer and use it in GitHub Desktop.
Create a Graphviz dot graph from all Ruby classes loaded
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
tree = {} | |
ObjectSpace.each_object(Class).each do |klass| | |
# All the Errno:: classes make the graph extremely large, | |
# so just collect them under a single name. | |
if klass.name.start_with?("Errno::") | |
ary = ["Errno::*"] | |
else | |
ary = [klass.name] | |
end | |
while klass = klass.superclass | |
ary.unshift(klass.name) | |
end | |
hsh = tree | |
while k = ary.shift | |
hsh[k] ||= {} | |
hsh[k][ary.first] ||= {} unless ary.empty? | |
hsh = hsh[k] | |
end | |
end | |
class Hash | |
def recursive_each(level = 0, &block) | |
each_pair do |k, v| | |
block.call(k, v, level) | |
v.recursive_each(level + 1, &block) | |
end | |
end | |
end | |
File.open("inheritance.gv", "w") do |file| | |
file.puts "digraph inheritance {" | |
tree.recursive_each do |k, v, level| | |
v.keys.each do |name| | |
file.puts("\"#{k}\" -> \"#{name}\";") | |
end | |
end | |
file.puts "}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment