From: Ken Bloom Date: 2007-12-14T00:25:03+09:00 Subject: Re: While statements in ruby On Tue, 11 Dec 2007 23:00:52 +0100, Robert Klemme wrote: > On 11.12.2007 18:25, Mark Mr wrote: >> Hi guys, I have a probably simple question. I dont know how to do >> iteration loops in ruby that reference more than one item of an array >> or more than one array. Here's an example of what I would do in php. >> Can anyone convert this to ruby? Thanks :) >> >> while ($i=0; $i < $response_table.length; $i += 2) { >> >> $response_table[$i] >> $response_table[$i + 1] >> >> } > > I am amazed nobody mentioned each_slice: with "enumerator" you can do: > > irb(main):006:0> (1..6).each_slice(2) {|a,b| print a,",",b,"\n"} 1,2 > 3,4 > 5,6 > => nil Because someone did mention #each_cons In reality, you're right and they (and most other people, myself included) didn't read closely enough to see that the loop does i+=2, not i+=1. --Ken -- Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/