Skip to content

Instantly share code, notes, and snippets.

@NathanielWroblewski
Last active January 1, 2016 13:39
Show Gist options
  • Select an option

  • Save NathanielWroblewski/8152346 to your computer and use it in GitHub Desktop.

Select an option

Save NathanielWroblewski/8152346 to your computer and use it in GitHub Desktop.
Ruby while loop example
#original
# DEF ask question
# puts question
# reply = gets.chomp.downcase
# WHILE (reply != 'yes' OR reply != 'no')
# puts 'Please answer with a yes or a no.'
# reply = gets.chomp.downcase
# END
# IF (reply == 'yes')
# wetsBed = TRUE
# ELSE
# wetsBed = FALSE
# END
# wetsBed
# END
# working examples:
# using and with while
def ask(question)
puts question
reply = gets.chomp.downcase
while reply != 'yes' && reply != 'no'
puts 'Please answer with a yes or a no.'
reply = gets.chomp.downcase
end
reply == 'yes'
end
# using or with until
def ask(question)
puts question
reply = gets.chomp.downcase
until reply == 'yes' || reply == 'no'
puts 'Please answer with a yes or a no.'
reply = gets.chomp.downcase
end
reply == 'yes'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment