ObjectGraph: a Ruby class inheritance hierarchy graph

  • Thread starter Mehr, Assaph (Assaph)
  • Start date
M

Mehr, Assaph (Assaph)

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 :)
* Call graph.rb (usage info below)
* Look at the pretty pictures :)


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/
 
S

Simon Strandgaard

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/.

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

--
Simon Strandgaard

server> 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 << "<variablelist>" #<title>Undtagelser</title>"
result.keys.sort.each do |parent|
childs = result[parent]
s << "<varlistentry><term><exceptionname>#{parent}</exceptionname></term>"
s << "<listitem><para>"
if childs.empty?
s << "Ingen undtageler er afledt heraf."
else
s << (childs.sort.map{|name| "<exceptionname>#{name}</exceptionname>"}.join(", ") + ".")
end
=begin
s << "<itemizedlist>"
s += childs.sort.map do |name|
"<listitem><para>#{name}</para></listitem>"
end
s << "</itemizedlist>"
=end
s << "</para></listitem></varlistentry>\n"
end
s << "</variablelist>"
File.open("ruby_exceptions.xml", "w+") do |file|
file.write s.join("\n")
end
server>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top