[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Loop question

Mark Mr

1/11/2008 8:33:00 PM

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 :)
--
Posted via http://www.ruby-....

12 Answers

Phlip

1/11/2008 8:56:00 PM

0

Mark Mr wrote:

> @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

@this_question.answers.each do |a|
if (a.answer == row[cell])
reply.reply = a.id
else
puts "Not Found"
end
end

Tip: Read most of a Ruby programming tutorial before further attempts! Just sit
away from computers and read it straight thru, like a book.

--
Phlip

Carlos J. Hernandez

1/11/2008 9:10:00 PM

0

Mark Mr:

Looks like mean you use if/else.

> @this_question.answers.each do |a|
> if (a.answer == row[cell])
> reply.reply = a.id
> else
> puts "Not Found"
> end
> end

BTW, I think ruby is probably the best programming language there is for
the beginner.
I say so with basic, assembly, fortran, C, javascript, java,
sh(ell)/bash, and perl in my past.
And nothing beats a good book to get you started, and I think
"Programming Ruby, The Pragmatic Programmer's Guide" is one of the best
introductions I've ever read.
You can even get a PDF version online, save a tree, and avoid a trip to
the book store.
Might as well be the bleeding edge version currently being written:
http://www.pragprog.com/ti...

Judson Lester

1/11/2008 9:34:00 PM

0

On Jan 11, 2008 12:33 PM, Mark Mr <pimea.mark@gmail.com> 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 :)
> --
> Posted via http://www.ruby-....

There are a couple of flow control tools you could use. You could
raise an exception, for instance, or return the value from a method,
and then puts "Not Found" if you complete the loop. Just because it's
lesser known, though, I'll point out:

catch :found do
@this_question.answers.each do |a|
if (a.answer == row[cell])
reply.reply = a.id
throw :found
end
end
puts "Not Found"
end

Judson
--
Your subnet is currently 169.254.0.0/16. You are likely to be eaten by a grue.

Carlos J. Hernandez

1/11/2008 10:01:00 PM

0

Doh! I see what he was asking now. I just don't read too good. :P

Todd Burch

1/11/2008 10:12:00 PM

0

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-....

Robert Klemme

1/11/2008 10:26:00 PM

0

On 11.01.2008 21:33, 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

Are you sure you have the flag evaluation in the right position? It
seems you want to be looking through @this_question.answers to find the
matching entry, don't you?

> 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 :)

ans = @this_question.answers.find {|a| a.answer == row[cell]}

if ans
reply.reply = ans.id
else
puts "nada"
end

Cheers

robert

Mark Mr

1/11/2008 11:11:00 PM

0

Ok so far i got the catch :found do method to work so thanks for that.
Todd, could you give me an example of how you might use .collect for
this? I tried doing it but I'm not quite sure i understand how to use it
in this situation. Keep in mind that for @this_question.answers.each,
every element of that is an answer object, not sure if that affects
methods that are performed on arrays.
--
Posted via http://www.ruby-....

Todd Burch

1/11/2008 11:33:00 PM

0

Mark Mr wrote:

> Todd, could you give me an example of how you might use .collect for
> this? I tried doing it but I'm not quite sure i understand how to use it
> in this situation. Keep in mind that for @this_question.answers.each,
> every element of that is an answer object, not sure if that affects
> methods that are performed on arrays.

Actually, Robert's example with .find should suffice. .find is also in
the Enumerable class.

If you REALLY want an example with .collect, I can write one up for
illustration purposes. The type of object in your array really does not
matter.

Todd

--
Posted via http://www.ruby-....

Mark Mr

1/14/2008 4:08:00 PM

0

Todd Burch wrote:
> Mark Mr wrote:
>
>> Todd, could you give me an example of how you might use .collect for
>> this? I tried doing it but I'm not quite sure i understand how to use it
>> in this situation. Keep in mind that for @this_question.answers.each,
>> every element of that is an answer object, not sure if that affects
>> methods that are performed on arrays.
>
> Actually, Robert's example with .find should suffice. .find is also in
> the Enumerable class.
>
> If you REALLY want an example with .collect, I can write one up for
> illustration purposes. The type of object in your array really does not
> matter.
>
> Todd

Ok well i tried again with .find and it wasnt working but then I
replaced it with .detect and it worked fine. So i guess there's a
difference there. Thanks for the help though :)
--
Posted via http://www.ruby-....

Todd Burch

1/14/2008 9:16:00 PM

0

Mark Mr wrote:

>
> Ok well i tried again with .find and it wasnt working but then I
> replaced it with .detect and it worked fine. So i guess there's a
> difference there. Thanks for the help though :)

Glad you got it working!
--
Posted via http://www.ruby-....