From: Martin DeMello Date: 2008-07-31T16:01:15+09:00 Subject: Re: deaf grandma. On Tue, Jul 29, 2008 at 2:51 PM, houston barnett-gearhart wrote: > > Martin, this keeps being brought up. If I'm understanding correctly, > after the Intermediary explains that questions towards Granny should be > in all caps if you want her to hear you, you want to additionally check > the inputted text (if it's in all caps) to check if "BYE" has been said. > But, logically that doesn't make any sense. Why would the first thing > you say to Granny, after it's been suggested you ask a question, be > "BYE"? It doesn't make sense to me.. which is why I believe I put the What you're missing is that, because it's a loop, the same code gets executed for every question. Consider the following code 1 loop do 2 granny_says(" say something") 3 reply = gets.chomp 4 if (reply == reply.upcase) 5 granny_says(" i hear you loud and clear") 6 else 7 granny_says(" eh? speak up!!") 8 end 9 end Now consider the transcript say something hello eh? speak up!! say something i said "hello" eh? speak up!! say something "hello"!!!!!!! eh? speak up!! say something HELLO! i hear you loud and clear say something Here it is again, annotated with the line of code being run 1 2 say something 3 hello 4 6 7 eh? speak up!! 2 say something 3 i said "hello" 4 6 7 eh? speak up!! 2 say something 3 "hello"!!!!!!! 4 6 7 eh? speak up!! 2 say something 3 HELLO! 4 5 i hear you loud and clear 2 say something Now if the user wants to say 'bye', which lines of code will handle it? Note that the only place where the program prompts for user input is lines 2 and 3. So we run line 2 2 say something and wait for user input 3 BYE the program will go to line 4, then to line 5, then skip 6,7,8 (the else block) and go to line 9 where it loops again. So the place to process the user's input and check if he has said "bye" is inside the if block. Ideally, instead of -- say "i hear you loud and clear" you want -- did he say "bye"? ---- was it the third time in a row? ------- if so, exit ------- if not, pretend you didn't hear so you want (1) an inner if/else block to replace line 5 and (2) logic within that block to keep track of whether we've seen three consecutive 'bye's The problem with using a separate loop is this: say something "hello"!!!!!!! eh? speak up!! say something HELLO! i hear you loud and clear say something BYE i'm afraid i didn't hear you properly - it almost sounded like you said 'bye' say something okay, I'm not leaving Now what? If you're inside a "wait for three byes" loop, you have no way of handling something that is *not* a BYE. So you need to have a single loop whose overall structure is loop do -- ask for input -- do something with input -- check if we need to exit ---- if so, exit ---- if not, make a response end And use variables for any tracking you need to do that spans multiple repeats of the loop >>If you let us know what platform you are on, people can recommend their >favourite editors and IDEs > > I'm on Windows XP. Recommend away! I'm on linux, so no real recommendations, I'm afraid. See if http://rubyonwindows.blogspot.com/2007/05/what-text-editor-should-i-use.html has anything helpful to suggest. Also, people's requirements vary; for me, autoindentation is the key feature of a good programming editor, followed by decent syntax highlighting. martin