From: Robert Klemme Date: 2010-12-27T20:39:46+09:00 Subject: Re: converting traversal fn into Enumeration On Sun, Dec 26, 2010 at 8:42 AM, Fearless Fool wrote: > I've not quite wrapped my head around Enumerations, but I suspect I'm > just being dense.  Assume the following small code fragment: > ----- > TNode = Struct.new(:left, :right, :value) > class TNode > >  def traverse(&block) >    left.traverse(&block) if left >    right.traverse(&block) if right >    yield(self) >  end Is this really the order that you want? Typically the current node is visited between left and right to get an in order tree traversal: def traverse(&block) left.traverse(&block) if left yield(self) right.traverse(&block) if right end If you need the result visiting the current node you can store it in a variable and return it at the end. Since you yield node instances themselves in #traverse it seems more natural to implement #each like this: class TNode include Enumerable def each(&b) left.each(&b) if left yield value right.each(&b) if right self # according to convention of #each end end > ... but as I mentioned, I haven't figured out the right way to structure > this.  Any hints?  Or is it not worth the effort? See above. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/