Graphing Ruby Objects
Jun 17, 2007 @ 11:36 amI’ve been working more on my ObjectGraph project, and I think I’m about ready to release. With a bunch of help from Ryan, and other Seattle.rb folks fixing my dumb errors, I’ve added a couple new features to Object Graph that I think people will enjoy.
First, you can no graph ascendants of your object. Say you have a reference, and you want to know the graph pointing to that reference. You can do it now:
class A; end
a = A.new
struct = { :one => [], :two => [a] }
grapher = ObjectGraph.new(:ascendants => true)
grapher.graph(a)
puts grapher
Which will produce this graph:
You can also see differences in graphs. Say you want to know what happens to your graph after performing some action. Just pass a block to the graph method, and ObjectGraph will diff the two trees. Green boxes are new objects, pink boxes are one that went away, and red arrows are references that were destroyed.
class A; end
struct = { :one => [], :two => [A.new]}
grapher = ObjectGraph.new
grapher.graph(struct) do |s|
s[:one] << A.new
s[:two].pop
end
puts grapher