This is the old RCRchive. It's available only for reading and reference. To submit RCRs for Ruby 1.9/2.0, and to participate in discussions about them, please visit the new RCRchive.
ruby picture

RCR 323: Object Graph Traversal in Standard Lib

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

Abstract

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

Problem

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.

Proposal

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

Analysis

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.

Implementation

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?

T.


I voted "strongly advocate", but I want to add a caveat that the interface should include an option for depth-first or breadth-first traversal. An optional parameter might work.

-sollaa


Strongly opposed0
Opposed0
Neutral0
In favor5
Strongly advocate6
ruby picture
ruby picture

Powered by Ruby on Rails.