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
@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:
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
···
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-forum.com/\.
--
Your subnet is currently 169.254.0.0/16. You are likely to be eaten by a grue.
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.
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]}
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.
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, 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
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
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
Yeah, they are aliases from what I read in the documentation but i
didn't change anything. I think it might be the fact that i already used
.find in the line, because here's the actual line of code i ended up
using.
ans = Question.find(@q_id_array[cell]).answers.detect {|a| a.answer ==
row[cell + @cell_spaces]}
if ans
reply.reply = ans.id
reply.save
end