Skip to content

Instantly share code, notes, and snippets.

@agentfin
Created January 29, 2012 22:04
Show Gist options
  • Save agentfin/1700935 to your computer and use it in GitHub Desktop.
Save agentfin/1700935 to your computer and use it in GitHub Desktop.
Chris Pine PragProg 9.4
# First version with a bunch of excess variables
# direct from Mr. Pine
def ask question
good_answer = false
while (not good_answer)
puts question
reply = gets.chomp.downcase
if (reply == 'yes' or reply == 'no' )
good_answer = true
if reply == 'yes'
answer = true
else
answer = false
end
else
puts 'Please answer "yes" or "no".'
end
end
answer # This is what we return (true or false)
end
puts 'Hello, and thank you for smoking.'
puts
ask 'Do you like pina-coladas?'
ask 'Do you like getting caught in the rain?'
risky_business = ask 'Do you know what your sig other likes?'
ask 'Are you into yoga?'
ask 'Do you have half a brain?'
puts 'Just a few more questions.'
ask 'Do like the feel of the ocean?'
ask 'Are you into champagne?'
puts
puts 'DEBRIEFING'
puts 'Thank\'s for all the fish'
puts
puts risky_business
# Nice little questionnaire
# any suggestions of TESTS that should be in here, greatly appreciated!
def ask question
reply = false
while ( ! reply )
puts question
reply = gets.chomp.downcase
if (reply == 'yes' || reply == 'no' )
reply = true
else
reply = false
puts 'Please answer "yes" or "no".'
end
end
end
puts 'Hello, and thank you for smoking.'
puts
ask 'Do you like pina-coladas?'
ask 'Do you like getting caught in the rain?'
risky_business = ask 'Do you know what your sig other likes?'
ask 'Are you into yoga?'
ask 'Do you have half a brain?'
puts 'Just a few more questions.'
ask 'Do like the feel of the ocean?'
ask 'Are you into champagne?'
puts
puts 'DEBRIEFING'
puts 'Thank\'s for all the fish'
puts
puts risky_business
@agentfin
Copy link
Author

The first one works fine. The challenge is to REMOVE 'answer' and 'good_answer' from the code.

That's where I'm getting locked up, since it seems I need to store or at least check 'reply' with each pass. Otherwise, my 'while' is just going over the same question over and over, rather than moving from one to the next.

Was able to get it to through all the questions, but when the 'yes/no' failed, the "Please answer yes or no" was being counted as a question and then skipping the next one in line.

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment