From: Martin DeMello Date: 2008-07-29T06:46:59+09:00 Subject: Re: deaf grandma. On Mon, Jul 28, 2008 at 1:32 PM, houston barnett-gearhart wrote: > > 3. (I cannot take ownership for writing this. sepp2k from #ruby-lang > helped me considerably. He alerted me to the fact I shouldn't be using > go-to style flow control & told me looping was the better bet.) This is actually shaping up very well. Here's a comment: > reply = "abc" > while reply != reply.upcase > puts > puts "Simon says, 'Input your text in all" > puts "caps! Otherwise, I won't echo back" > puts "anything you say! Instead, I will" > puts "spit out my 'Simon says' spiel once" > puts "more. & you wouldn't want that!" > puts > reply = gets.chomp > end > > puts reply The algorithm for this is: loop ---- get a reply ---- if the reply is not in all caps ------ repeat loop ---- otherwise ------ exit loop You're not doing anything with the reply yet. The following skeleton might be easier to work with 0 loop 1 ---- get a reply 2 ---- if the reply is in all caps 3 -------- do something 4 ---- otherwise 5 -------- do something else 6 ---- repeat loop Note that steps 1-5 simply get a reply and deal with responding to it, and that they are contained cleanly inside the loop body. Of course, you do want to exit the loop at some point, but I'd suggest doing it this way: 0 loop 1 ---- get a reply 2 ---- if the reply is in all caps 3 -------- do something 4 ---- otherwise 5 -------- do something else 6 ---- break out of the loop if we need to 7 ---- repeat loop (unless we've broken out) Ruby does support the 'break' call to exit a loop, but another, and often helpful way to do things is to have a variable act as a flag, and check the flag each time to see whether we loop again. Then, within the body of the loop, you signal "ready to exit" by setting the flag. Here's some code that demonstrates both approaches: # one way: use a flag to end the loop done_talking = false while (not done_talking) do puts "Loop 1> Say something (or 'quit' to exit)" reply = gets.chomp if (reply.downcase == "quit") done_talking = true else if (reply == reply.upcase) puts "that was in all caps" else puts "that was not in all caps" end end end puts "exited loop 1" # and the other way: loop infinitely, but break out if needed while true do puts "Loop 2> Say something (or 'quit' to exit)" reply = gets.chomp if (reply.downcase == "quit") break else if (reply == reply.upcase) puts "that was in all caps" else puts "that was not in all caps" end end end puts "exited loop 2" # an advantage of the 'break' method is that it saves one level of if/else while true do puts "Loop 3> Say something (or 'quit' to exit)" reply = gets.chomp if (reply.downcase == "quit") break end if (reply == reply.upcase) puts "that was in all caps" else puts "that was not in all caps" end end puts "exited loop 3" > > ... for now, that's all I've got. I've been working on each individual > step of the iterative approach & trying to pick up what I can through my > questions on #ruby-lang. I'll post the code for 4 & 5 later on in the > day. Please take a look over what I've written (with the help of an > array of people) & let me know if there's anywhere I can clean it up. > Thanks! No problem :) Looking forward to seeing the next two parts. martin