Skip to content

Instantly share code, notes, and snippets.

@xoebus
Created September 15, 2010 14:51
Show Gist options
  • Save xoebus/580837 to your computer and use it in GitHub Desktop.
Save xoebus/580837 to your computer and use it in GitHub Desktop.
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
if answer.empty? and default
answer = default
matches = true
end
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