Skip to content

Instantly share code, notes, and snippets.

@eregon
Created July 8, 2010 10:01
Show Gist options
  • Save eregon/467825 to your computer and use it in GitHub Desktop.
Save eregon/467825 to your computer and use it in GitHub Desktop.
Ceci est un essai de réponse en Ruby à:
http://lasts.goldzoneweb.info/pf.html
class ValidationError < StandardError; end
def ask(question, answer_type = String, not_valid = nil, &validation)
begin
print "#{question} : "
answer = send(answer_type.name, gets.chomp)
raise ValidationError unless validation[answer]
answer
rescue ValidationError
puts not_valid if not_valid
retry
rescue
puts "Veuillez entrer un #{answer_type}"
retry
end
end
ask("Entrez votre nom") { |nom| nom.size > 0 }
ask("Entrez votre âge", Integer,
"Vous ne pouvez pas avoir un âge négatif.") { |age| age > 0 }
ask("Entrez votre mot de passe", String,
"Votre mot de passe doit faire au moins six caractères.") { |mdp| mdp.size >= 6 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment