From: Todd Burch Date: 2008-01-12T07:12:09+09:00 Subject: Re: Loop question Mark Mr wrote: > I'm trying to figure out an easier way to work with loops in ruby. > Basically this is what i have now > > > @this_question.answers.each do |a| > missing_flag = true > if (a.answer == row[cell]) > reply.reply = a.id > missing_flag = false > end > if (missing_flag) > puts "Not Found" > end > end > > Is there any way to avoid using this flag method? I want to put a > condition on the .each statement that says if no a.answers = row[cell] > then dont do the reply.reply = a.id. I cant do an else because it would > run the else after every element in the answers, and i only want it to > be done after all of them have been run. BTW i'm kind of new to ruby. > Any suggestions? thanks :) Without writing a test case to try it out myself, I'm thinking you could use .collect (or .map) to do what you want to do. If any condition (a.answer==row(cell)) were true, you could return it from .collect, and at the end, if anything were returned, do the message thing you need to do. Look in the book mentioned above at the Enumerable class. Very handy for this sort of thing. Todd -- Posted via http://www.ruby-forum.com/.