From: James Edward Gray II Date: 2005-03-22T10:57:35+09:00 Subject: Re: stepping through arrays simultaneously On Mar 21, 2005, at 7:51 PM, tsuraan wrote: > Suppose I have two arrays, one holding times, and the other holding > events corresponding to the times. I want to step through both arrays > at the same time. Is there an elegant way to do this? For example: > > times = [ '1', '2', '3', '4', '5'] > events = [ 'eat', 'sleep', 'eat', 'play', 'sleep' ] > > times.size.times { |cnt| > puts times[cnt] + ": " + events[cnt] > } > > That's not too pretty; at least, I think the times.size.times {block} > is ugly. For that, I would use Array#each_index(). > Is there a way to join the arrays into an array of n-tuples and step > through them one tuple at a time? Sure is. You want Array#zip(). Hope that helps. James Edward Gray II