Loop question

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

···

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

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:

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

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

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

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

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

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

Cheers

  robert

···

On 11.01.2008 21:33, Mark Mr wrote:

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

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-forum.com/.

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-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/\.

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

Glad you got it working!

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On 14.01.2008 17:08, Mark Mr wrote:

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

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

···

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