Visually Inspect Ruby Object Models with DrX
When you want to inspect your objects in Ruby,
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:
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
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.