Ruby Class Hierarchy to DGML


I am having some fun learning Ruby. I’m using the Visual Studio 2010 Ruby IDE from which makes things a lot more fun than using some silly interactive console window. They did a nice job.

Ruby is a strange little language. I’m quickly coming to think Ruby was deliberately designed to borrow as much stuff from as many different languages as possible. The ultimate mutt of programming languages.

The thing that shocks me the most about Ruby is that all the strings are not Unicode. They say they are fixing that in Ruby 2.0, but that’s not out till 2013. Amazing that a popular language this day and age would even dare to exist without native Unicode support.

I love DGML as you know, so I wrote the following code using the Ruby Symbol table to find all classes and generate a DGML graph of the Ruby class hierarchy:


# This function generates a DGML graph from the superclass information in all known classes.


class ClassHierarchy


       # This method finds all classes and calls the given handler passing the class.

       # (The “&” prefix here means the argument is a procedure)

       def AllClasses(&handler)

              # use the “each” method which takes a procedure (the stuff inside curly braces)

              # the |s| is the closure variable that binds to the “each” iterator.



                name = s.id2name


                # classes begin with uppercase letters

                # (notice the native support for regex in the language)

                if (name =~ /[A-Z].*/) then


                             # use eval to execute the code, which is just a symbol

                             # reference, so it returns the Class instance.

                             c = eval(name)

                             # if it’s really a class then it should have a superclass

                             # method

                             sc = c.superclass

                             if (sc != nil) then

                                    # now pass this to our handler.



                           rescue Exception

                             # exceptions here mean we found something that was not

                             # a class, so ignore it.






       # Generate a DGML graph linking every class with it’s superclass

       def GenerateDgml()

         puts(“<DirectedGraph xmlns=’’>”)

         puts(  <Links>”)


         # use our AllClasses method, passing a procedure that prints out a

         # DGML <Link> for each class found, including a link to it’s super class.

         AllClasses{|c| puts(    <Link Source=’#{}’ Target=’#{}’/>”) }


         puts(  </Links>”)





browser =



This produces the following DGML graph: