On 9/8/06, A. S. Bradbury <asbradbury / tekcentral.org> wrote:

> A more general question - how would you (ruby-talk readers) implement this
> method? Two people on #ruby-lang suggested I make it recursive, but I really
> don't see this sort of tree traversal mapping to a recursive method, am I
> wrong?

Tree traversal usually lends itself well to a recursive
implementation, but it's not clear to me exactly what your each_level
method is supposed to do.


But here's a recursive implementation that might do the same thing.

def each_level(include_self=false, &block)
      yield [self] if include_self
      kids = children.values
      unless kids.empty?
           yield kids
           kids.each do {|kid| kid.each_level(&block)}
      end
end

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/