Want to stay on top? Ruby Weekly is a once-weekly e-mail newsletter covering the latest Ruby and Rails news.
     Feed Icon

Visually Inspect Ruby Object Models with DrX

By Peter Cooper / April 28, 2010

When you want to inspect your objects in Ruby, Object#inspect, p, or awesome_print are all valuable. You're stuck with plain-text, though, and primarily designed to look at object data rather than object models. If you want to drill down into parent classes, see object and class relationships, etc, then, check out DrX, a visual object inspector for Ruby!

DrX bills itself as a "small object inspector", but its key features are that it shows results visually (in a GUI interface) and that it focuses on showing the object model behind your objects, rather than the data contained within. A visual example of a DrX session should give you the idea:

Usage

Once DrX is installed (more on that in the next section), you just require 'drx' it into your app (or even within irb) and then use the Object#see method to get DrX into action:

require 'drx'
123.see

Even this rudimentary example will bring up an interesting graph. The DrX author does, however, provide a more interesting example to show off DrX's introspective features:

s = "wizard of oz"
def s.strong
"<strong>" + self + "!</strong>"
end
s.see

Installation and Prerequisites

Depending on your setup, DrX might take some serious work to get going. If you're running Linux and are heavy on your development experimentation, you might have everything ready to go. Just try gem install drx and see if the above examples work in irb.

Failing that, DrX uses Tk for its GUI work in order to be cross-platform and also requires GraphViz to be present. Install these with your package manager of choice and ensure that your Ruby installation has the Tk bindings installed (again, easier said than done).

On OS X 10.6 (Snow Leopard) I discovered that the stock Ruby installation does not include the Tk bindings, even though Tk is present otherwise. Rather than mess it up, I relied on the always-wonderful RVM and installed Ruby 1.9.2-preview1 (rvm install ruby-1.9.2-preview1). With this, Tk worked "out of the box" and gem install rbx was OK. For the Graphviz dependency, sudo port install graphviz did the trick and also "just worked." If you're one of the anti-Macports crowd, though, you might need to find a different approach.

Comments

  1. Harold Giménez says:

    brew install graphviz + your 1.9.2 preview1 trick worked fine here

  2. John Wells says:

    I ran across a few issues getting it running on Ubuntu Lucid. I posted a short write-up here: http://bit.ly/d60Dee

  3. Jeff says:

    Setting up everything from scratch on OS X:

    1) Install Homebrew:
    sudo mkdir /usr/local
    sudo chown -R yourusername /usr/local
    curl -L http://github.com/mxcl/homebrew/tarball/master | tar xz --strip 1 -C /usr/local

    2) Install RVM
    bash < > ~/.bash_profile
    source "$HOME/.rvm/scripts/rvm"

    3) Install 1.9.2
    rvm install 1.9.2-preview1
    rvm use 1.9.2

    4) Install GraphViz
    brew install graphviz

    5) Install drx
    gem install drx

    Ta-Da!

  4. mgutz says:

    seems to work with 1.9.2-head as well

  5. Peter Cooper says:

    I tried 1.9.2-head too, but it seems to be broken install-wise with RVM right now (tried a fresh install and everything) :-( If you can get it running though, good to know.

  6. Yi Wen says:

    Actually, ruby 1.8.7 works just fine. don't have to use 1.9.2 on SL

  7. Peter Cooper says:

    Random test.

Other Posts to Enjoy

Twitter Mentions