Re: deaf grandma.



On Mon, Jul 28, 2008 at 1:32 PM, houston barnett-gearhart
<americanpragmatic@xxxxxxxxx> 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

.



Relevant Pages

  • Filehandling Problem
    ... flagging over the filesystem. ... I have a process that runs a loop creates a file and sleeps a while: ... puts "Starting Process 1" ...
    (comp.lang.ruby)
  • Re: (beginner) Substitution - braces and double quotes
    ... the $vars get substituted the moment Tcl reads the string. ... defer this substitution. ... not understand is that, after line 5 (first passage of while loop), ... of puts in my first message), even if at the end the loop ends as ...
    (comp.lang.tcl)
  • [SUMMARY] Word Loop (#149)
    ... cheater is a wonderful label for a programmer to have. ... the first priority is to find a possible loop. ... puts before+letter+looplets.shift ...
    (comp.lang.ruby)
  • Re: TCL Need help
    ... I have a simple script like this. ... puts i=$i;set i 11} ... enters into indefinite while loop which according to one of the book ... being evaluated at the beginning leading to an infinite loop. ...
    (comp.lang.tcl)
  • Re: Help With Homework Code
    ... puts "Would you like to Roll or Pass?" ... The empty parentheses are typically omitted. ... you could also simply call 'break' here and use the 'loop' ...
    (comp.lang.ruby)