Created
September 15, 2010 12:06
-
-
Save xoebus/580636 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ask(question, default=nil, valid = /.*/, multiline=false) | |
output = question | |
output << " [#{default}]" unless default.nil? | |
output << " (Optional)" if valid =~ "" | |
output << ": " | |
puts output | |
matches = false | |
answer = default | |
while not matches | |
if multiline | |
answer = "" | |
while not (input = gets).nil? | |
answer << input | |
end | |
else | |
answer = gets.chomp | |
end | |
matches = answer =~ valid | |
unless matches | |
puts "The entered input is invalid. Please try again: " | |
end | |
end | |
answer | |
end | |
man = ask "Are you a man???", "Y/N" | |
puts man |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment