"Claus Spitzer" <DocBoobenstein / gmail.com> schrieb im Newsbeitrag
news:a7c2a819040723064612cd7b83 / mail.gmail.com...
> I see your point. While ObjectSpace.each_object can be limited to
> objects of a certain class or module, it doesn't help when neither is
> certain. On the other hand, would it be possible to extend ObjectSpace
> to keep such lists? I'm just going with the reuse, reduce, recycle
> principle

That's the wrong place IMHO.  You want application dependend traversal for
which there is no general mechanism.  Better put it into application code.

Btw, Marshal and YAML do already such a thing (object graph traversal).
Maybe there is some code that you can reuse.  Other than that, it can be
as simple as:

def traverse(obj)
  seen = {}
  queue = [obj]
  enq = lambda {|x| queue.push x unless seen[x.id]}

  until queue.empty?
    o = queue.shift
    seen[o.id]=true
    yield o

    case o
      when Hash
        o.each {|k,v| enq[ k ]; enq[ v ]}
      when Enumerable
        o.each {|e| enq[ e ] }
      else
        o.respond_to? :instance_variables_get and
o.instance_variables_get.each do |iv|
          enq[ o.instance_variable_get( iv ) ]
        end
    end
  end
end

traverse( 1=>2, 3=>4 ) do |o|
  p o
end

Note: there might be some additional cases I didn't take care of.

Regards

    robert