From: "Kroeger, Simon (ext)" Date: 2006-06-07T23:57:11+09:00 Subject: Re: Getting rid of a while loop > -----Original Message----- > From: Robert Klemme [mailto:shortcutter@googlemail.com] > Sent: Wednesday, June 07, 2006 3:47 PM > To: ruby-talk ML > Subject: Re: Getting rid of a while loop > > 2006/6/7, James Edward Gray II : > > On Jun 7, 2006, at 7:53 AM, Tait Pollard wrote: > > > > > I get a feeling there should be another way to make this happen > > > without > > > using a while loop, if anyone has any ideas that'd be great. > > > > > > while question=file.gets > > > answer=file.gets > > > > @question_array[counter]=Questions.new(question,answer, false) > > > counter+=1 > > > end > > > > One way would be: > > > > require "enumerator" > > > > # ... > > > > file.each_slice(2) do |question, answer| > > @question_array << Questions.new(question, answer, false) > > end > > Thanks for leaving the inject version for me. :-) > > @question_array = file.to_enum(:each_slice, 2).inject([]) do > |qa,(quest, answ)| > qa << Questions.new(quest, answ, false) > end > > :-)) > > robert hehe, nice, with 1.8.4 @question_array = file.enum_slice(2).map{|q, a| Questions.new(q, a, false)} should be possible, isn't it? cheers Simon