Last active
August 29, 2015 14:15
-
-
Save SebDeclercq/69bf1839e7acc8fd1da7 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
#!/usr/bin/ruby1.9.3 | |
# -*- coding: utf-8 -*- | |
class Humeur | |
attr_accessor :depression | |
attr_accessor :anxiete | |
attr_accessor :irritabilite | |
attr_accessor :excitation | |
attr_reader :moment | |
attr_reader :id | |
@@id = 0 | |
def initialize (depression,anxiete,irritabilite,excitation) | |
@id = @@id | |
@moment = Time.now | |
champ = { depression: depression, anxiete: anxiete, | |
irritabilite: irritabilite, excitation: excitation } | |
champ.each do |k,v| | |
controle(k,v) | |
end | |
@@id += 1 | |
end | |
private | |
def controle (champ,valeur) | |
if valeur.is_a? String | |
equivalence = { "aucune" => 0, "faible" => 1, "modérée" => 2, "forte" => 3 } | |
equivalence.each do |k,v| | |
instance_variable_set( "@#{champ}", v ) if valeur == k | |
end | |
erreur(champ,valeur) if instance_variable_get("@#{champ}").nil? | |
elsif valeur.is_a? Integer | |
if (0..3).include? valeur | |
instance_variable_set( "@#{champ}", valeur ) | |
else | |
erreur(champ,valeur) | |
end | |
else | |
erreur(champ,valeur) | |
end | |
end | |
def erreur (champ,valeur) | |
abort "Erreur : la valeur \"#{valeur}\" n'est pas autorisée dans le champ \"#{champ}\"." | |
end | |
end | |
puts "Veuillez insérer votre humeur ( dépression, anxiété, irritabilité, excitation )" | |
str = gets.chomp | |
vals = str.split(/ |,/) # SOUCI : split génère une chaine même avec les chiffres ! | |
humeur = Humeur.new( *vals ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment