ruby picture

RCR 323: Object Graph Traversal in Standard Lib

Submitted by Robert (Thu Oct 27 08:31:56 UTC 2005)


Make a standard library function available that starts with a given object and traverses all objects reachable from there properly dealing with recursion.


It comes up frequently on ruby-talk that people want to traverse object graphs to do something with each instance. Although there are certainly specialized traversals needed a great deal of them just visits every instance and does something with it.


Add a method (for example Kernel.traverse(obj,&b)) which receives a starting object and a block. It traverses through the object graph and yields every instance found at most once to the block. Proper handling of recursive structures included of course. An alternative interface might be Kernel.traverse(*objs,&b).


It's more efficient to have this in C code and only once so everybody can use it. Also it seems a fairly general thing to do. Module pp might also benefit.


The code should be there already (in Marshal), there's just another interface to it needed.
ruby picture
Comments Current voting
Very good idea, this will be useful.

I'd stick it into ObjectSpace.

Seems reasonable. Anyone want to write a Ruby version in the mean time? ObjectSpace#traverse.

While were at it why isn't #each_object just called #each and Enumerable included? Ah...becuase it takes a parameter -- Enumerable should dela with that, but Matz has said "no". :( Reconsider?


Strongly opposed 0
Opposed 0
Neutral 0
In favor 5
Strongly advocate 4
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .