From: Belorion Date: 2005-04-02T08:08:53+09:00 Subject: Synchronous but different iterators (somewhat contrived example:) Say I have 2 different data structures which I want to iterate over, and Tree, and an Array. I had to write specific iterators for my Tree structure, such as depth_first, breadth_first, etc. Is there a good way to iterate over each structure in a synchronous manner, but with a different iteration style? For example, I want to step through my Array normally using each_index, but want to step through my Tree structure using, say, depth_first, but I want to do this concurrently because of what I want to do to/with the data. "pseudo" ruby code: (myTree.depth_first.each, myArray.each_index){ |tree_node, array_element| # do stuff with each in tandem } Is there a trick to doing this? Or is my best bet to iterate over my Tree object and create a temporary array, and then just iterate in step using with_index? tmpArr = [] myTree.depth_first{ |node| tmpArr.push node } myArray.each_index{ |ii| tree_node, array_element = tmpArr[ii], myArray[ii] } The first approach would raise some questions such as what to do if one iterator reaches the end before the other ... and one would have to decide wether the "next" object in that iteration is nil, or if the synchronous iterator terminates whenever the shortest iteration reaches the end. Obviously, the (2nd) working ruby solution is not that difficult, but I am more curious from an academic standpoint if the first approach is possible through some fancy yield tricks.