From: Colin Bartlett Date: 2010-04-04T10:26:03+09:00 Subject: Re: combined ranges... On Sun, Apr 4, 2010 at 1:12 AM, Caleb Clausen wrote: > On 4/3/10, Intransition wrote: >> The more OOP approach would be: >> >>   class Object >>     def visits >>       yield(self) >>     end >>   end >> >>   module Enumerable >>     def visits(&block) >>       each{ |item| item.visits(&block) } >>     end >>   end > > This won't behave well if passed a true graph (not just tree or dag) > of ruby objects. For instance: >  a=[] >  a<  a.visits{|x| p x } > > At one time, I got interested enough in this problem to write a fairly > complete solution to this problem (at least I think so). If > interested, please have a look at: >  http://github.com/coatl/ron/blob/master/lib/ron/graphedge.rb So, temporarily ignoring the problem of possible infinite loops (which I'd just begun to worry about - I was using a = [2] ; a << a, which in IRB gives [2, [...]], and a = a[1] #=> [2, [...]], but your example is even simpler) you opted for recursive_each (and recursive_reverse_each)? Going back to the original problem, my understanding is that Roger Pack was looking for a simple way of putting some data into an array and then getting at it, in which case the likely maximum depth may well be known, and then Robert Klemme's solution with a mandatory level/depth argument seems to work. So I withdraw my suggestion of level/depth = nil for infinite depth! (Interestingly, Numeric#step raises an error if the step is 0, but Date#step seems to allow infinite loops with a step of 0.)