The first and perhaps the most natural use of intervals is to express a sequence. Ruby creates these strings using the range operators:
".." - Creates an inclusive range. Ex:
(1..5).to_a => 1,2,3,4,5
('a'..'d').to_a => 'a','b','c','d'
"..." - Create an interval that excludes the specified high value. Ex:
(1...5).to_a => 1,2,3,4
Intervals can also be used as conditional expressions.
Ex:
while gets
print if /star/../end/
end
score = 70
result = case score
when 0..40 then "Fail"
when 41..60 then "Pass"
when 61..70 then "Pass with Merit"
when 71..100 then "Pass with Distinction"
else "Invalid Score"
end
puts result
=> Pass wuith Merit