ObjectGraph: a Ruby class inheritance hierarchy graph

What the…?

···

==========

A simple script that generates a graph of the ruby class hierarchy.
The script relies on GraphViz[1] for generation of the PNG and HTML map
files.
Take a look at the basic Ruby class hierarchy on the project web site:
http://objectgraph.rubyforge.org/.

Why?

Because it’s neat. And because the online PickAxe seems to be missing
some graphs. And because many nubys (myself included) don’t always know
the Exception inheritance hierarchy. And because I was home sick and
needed something to do (which is a nice excuse why the code may not be
all that great :).

Features:

  • Supports multiple layout engines (dot, neato, twopi and circo).
  • Supports multiple output formats by GraphViz (PNG, JPEG etc.)
  • Errno::… classes can be omitted.
  • Clicking on a class node should go to the ruby-doc.org documentation
    for that class.

Installation:

  • Install GraphViz and make sure that the executables are on the path.
  • Grab the graph.rb file (CVS Only at this stage. I’ll release a zip
    that includes some pics later :slight_smile:
  • Call graph.rb (usage info below)
  • Look at the pretty pictures :slight_smile:

Next on the TODO list:

  • Add capability to generate only partial tree, based on a common base
    class (e.g. Exception).
  • Add capability to generate a graph based on name-space (e.g. Wx).
  • Support links to std-lib classes documentations.

Cheers,
Assaph

[1] http://www.research.att.com/sw/tools/graphviz/

I have done something similar which builds a tree of the exception
hierarchy for my book. Output is DocBook format.

See attached for code for details.

output can bee seen on
http://neoneye.dk/rubybook/exceptions.html

···

On Thu, 15 Apr 2004 14:48:18 +0900, Mehr, Assaph (Assaph) wrote:

A simple script that generates a graph of the ruby class hierarchy.
The script relies on GraphViz[1] for generation of the PNG and HTML map
files.
Take a look at the basic Ruby class hierarchy on the project web site:
http://objectgraph.rubyforge.org/.


Simon Strandgaard

expand -t2 ruby_exceptions.rb
parents =
ObjectSpace.each_object do |obj|
if obj.class == Class and obj.superclass == Exception
#puts “class-derived-from-exc #{obj}”
parents << obj
end
end
#p parents

result = {}
parents.each {|klass| result[klass.to_s] = }

ObjectSpace.each_object do |obj|
parents.each do |cls|
if obj.class == Class and obj.superclass == cls
#puts “child of #{cls}: #{obj}”
result[cls.to_s] << obj.to_s
end
end
end

require ‘pp’
pp result

s =
s << “” #Undtagelser"
result.keys.sort.each do |parent|
childs = result[parent]
s << “#{parent}”
s << “”
if childs.empty?
s << “Ingen undtageler er afledt heraf.”
else
s << (childs.sort.map{|name| “#{name}”}.join(“, “) + “.”)
end
=begin
s << “”
s += childs.sort.map do |name|
“#{name}”
end
s << “”
=end
s << “\n”
end
s << “”
File.open(“ruby_exceptions.xml”, “w+”) do |file|
file.write s.join(”\n”)
end