From: tsuraan Date: 2005-03-22T11:04:38+09:00 Subject: Re: stepping through arrays simultaneously On Mar 21, 2005, at 8:57 PM, James Edward Gray II wrote: > 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(). Ok, that looks good. I missed it :) >> 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(). I remember seeing that in pickaxe 2, but it's not in the documentation of Array. It's in the online docs of course, but it's missing from the book. Strange. Thanks though; I like that function. > > Hope that helps. Very much! Thanks! --jay