Loop question

M

Mark Mr

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

Phlip

Mark said:
@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.
 
C

Carlos J. Hernandez

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/titles/ruby3
 
J

Judson Lester

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

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
 
T

Todd Burch

Mark said:
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
 
R

Robert Klemme

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
 
M

Mark Mr

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

Todd Burch

Mark said:
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
 
M

Mark Mr

Todd said:
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 :)
 
T

Todd Burch

Mark said:
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!
 
R

Robert Klemme

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

As far as I know #detect and #find are aliases so I suspect you changed
something else, too.

irb(main):001:0> a=%w{foo bar baz dada}
=> ["foo", "bar", "baz", "dada"]
irb(main):002:0> a.detect {|s| s.length == 4}
=> "dada"
irb(main):003:0> a.detect {|s| s.length == 3}
=> "foo"
irb(main):004:0> a.find {|s| s.length == 3}
=> "foo"
irb(main):005:0> a.select {|s| s.length == 3}
=> ["foo", "bar", "baz"]
irb(main):006:0>

Kind regards

robert
 
M

Mark Mr

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

So maybe thats why, but detect works just fine :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top