Range validation with a Case statement in Ruby
1 score = 70 2 3 result = case score 4 when 0..40: "Fail" 5 when 41..60: "Pass" 6 when 61..70: "Pass with Merit" 7 when 71..100: "Pass with Distinction" 8 else "Invalid Score" 9 end 10 11 puts result
DZone Snippets > jrobertson > switch
13525 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
James Robertson http://www.r0bertson.co.uk
1 score = 70 2 3 result = case score 4 when 0..40: "Fail" 5 when 41..60: "Pass" 6 when 61..70: "Pass with Merit" 7 when 71..100: "Pass with Distinction" 8 else "Invalid Score" 9 end 10 11 puts result