From: Dark Ambient Date: 2006-06-26T00:16:01+09:00 Subject: Re: Stumped:beginners question If I'm assimilating this correctly then if something is not equating to false then it automatically equates to true. Which makes sense. However, sadly I must say setting up the loop is still a problem for me to grasp. (Maybe I should consider another hobby :)) Here it's new incarnation I'm still failing to catch the error and repeat the question. def ask question reply = false while not reply puts question reply = gets.chomp.downcase case reply when 'yes': reply = true when 'no' : reply = false else puts 'please answer "yes" or "no"' end end end Note - When I attempted to use this: mexicanfood.rb:10: parse error, unexpected ':', expecting kEND when 'no' :return = false I'm seriously wondering if this obstacle to my understaning is an indicator of my innate skills or if spending more time will help to enlighten and break through. Stuart > while true > puts question > case gets.chomp.downcase > when 'yes' : return true > when 'no' : return false > else puts 'please answer "yes" or "no"' > end > end ruby was spitting out an error: On 6/25/06, Matthew Smillie wrote: > On Jun 25, 2006, at 14:45, Rob Biedenharn wrote: > > > On Jun 25, 2006, at 9:32 AM, Dark Ambient wrote: > > > >> while (not reply) > >> if (reply !='yes' || reply != 'no' ) > >> puts ' Please answer "yes" or "no".' > >> elsif reply == 'yes' > >> reply = true > >> else reply == 'no' > >> reply = false > >> end > >> end > > > > Try: > > if (reply != 'yes' && reply != 'no') > > > > OR: > > > > case reply > > when 'yes': reply = true > > when 'no' : reply = false > > else puts 'please answer "yes" or "no"' > > end > > > > In your original, reply is going to be != to at least one of the > > two alternatives! > > There's trouble with your loop condition as well: you'd want to > terminate the loop when reply == false, but when reply == false, the > loop won't terminate, since !reply == true. Similarly, when you get > a bad answer, reply == "foo" (or something), so !reply == false, and > the loop will terminate. > > In this case, I'd use a case statement like R.B. suggested, but with > an explicit return to break the loop: > > while true > puts question > case gets.chomp.downcase > when 'yes' : return true > when 'no' : return false > else puts 'please answer "yes" or "no"' > end > end > > Or, if you don't like explicit returns, maybe something like this: > while (not reply) > puts question > reply = gets.chomp.downcase > if (reply != 'yes' && reply != 'no') > puts 'please answer "yes" or "no"' > redo # restarts the loop without checking the condition > end > end > # convert yes/no to true/false here so that it doesn't > # interfere with the loop. > reply == 'yes' ? true : false > > > > matthew smillie. > >