Hypothetical case statement below that takes into account three variables:
- name
- age
- garlic_allergy
With the case statement below, the goal is to have the "result" variable update to the LATEST condition, rather than the first condition match. e.g. If the user were to input variables age=90, garlic_allergy=false, name=Dracula, the case statement should return "Definitely a vampire" (the LATEST condition met) as opposed to "Probably NOT a vampire" (the first condition met).
case
when age.to_i < 100 && garlic_allergy = false
result = "Probably NOT a vampire"
when age.to_i < 100 && garlic_allergy = true
result = "Maybe a vampire"
when age.to_i > 100 && garlic_allergy = true
result = "almost certainly a vampire"
when name = "Dracula"
result = "Definitely a vampire"
else
result = "Results inconclusive"
end
puts result
end
This is an example case, but trying to figure out how to do this for any case statement or if/elsif/else statement with multiple conditions, etc. ... how to make sure the program runs through every when/elsif statement and returns the latest condition. I tried doing it by wrapping the statement in a loop but haven't had any luck yet.
Aucun commentaire:
Enregistrer un commentaire