Ruby Class Hierarchy to DGML

by clovett27. January 2012 08:49

I am having some fun learning Ruby. I'm using the Visual Studio 2010 Ruby IDE from www.sapphiresteel.com 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 following diagram illustrates what Ruby borrowed from where:

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.

              Symbol.all_symbols.each{

              |s|

                name = s.id2name

 

                # classes begin with uppercase letters

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

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

                            begin

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

                                    handler.call(c)

                             end                 

                           rescue Exception

                             # exceptions here mean we found something that was not

                             # a class, so ignore it.

                           end

                 end

              }

       end

      

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

       def GenerateDgml()

         puts("<DirectedGraph xmlns='http://schemas.microsoft.com/vs/2009/dgml'>")

         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='#{c.name}' Target='#{c.superclass.name}'/>") }

 

         puts("  </Links>")

         puts("</DirectedGraph>")

       end

end

 

browser = ClassHierarchy.new

browser.GenerateDgml();

 

 

This produces the following DGML graph

See the following DGML file: RubyClasses.dgml (9.71 kb)

Tags:

Comments (1) -

plastic surgeon Santa Rosa medical center
plastic surgeon Santa Rosa medical centerUnited States
12/1/2014 6:39:14 PM #

I was suggested this website by my cousin. I'm not sure whether this post is written by him as no one else know such detailed about my difficulty. You are amazing! Thanks!

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

Chris Lovett is a Software Engineer at Microsoft working on Windows Phone.

See Resume in SVG

Month List