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.