Create a graph of all defined Classes
i wanted to see ruby classes and each class connected to its parent
i used the ruby-graphviz, it turns out that there is another gme named graphviz and it is not the one tha will work correctly.
my method was fairly easy, i get all defined constants then filter them to get classes names only, after that i add a node for each class, then iterated on them to add edges between every class and its parent, and then i saved graph as png and pdf, an output example looks like this http://imgur.com/Z1LfLn1
Gist : https://github.com/blazeeboy/RubyScripts/tree/master/2014-4-28
#!/usr/bin/env ruby
# Author : Emad Elsaid (https://github.com/blazeeboy)
#
# Output example :
# http://imgur.com/Z1LfLn1
#
# install ruby-graphviz then install graphviz from
# http://graphviz.org/
require 'graphviz' # gem install ruby-graphviz --no-document
# get all constants in namespace
# then convert them from symbols
# to their objects and select
# the classes out of them,
# you may include modules if you wish
classes = Class.constants
.map { |c| Class.const_get c }
.select { |c| c.is_a? Class }
g = GraphViz.new( :G, :type => :digraph )
# Create nodes
nodes = Hash[classes.map {|c| [c, g.add_nodes( c.to_s )] }]
nodes.each do |klass,node|
begin
g.add_edges node, nodes[klass.superclass]
rescue # do nothing
end
end
# Generate output image
g.output( :png => "/Users/blaze/Desktop/classes_graph.png" )
# you can output PDF with small modification
g.output( :pdf => "/Users/blaze/Desktop/classes_graph.pdf" )
Written by Emad Elsaid
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#