I am not personally convinced that you wouldn't be better off with if statements, but if you want a solution in that form:
Inf = 1.0/0
case myvar
when -Inf..-5
do somethingA
when -5..-3
do special_something_XX
when -2..-1
do special_something_YY
when 0
do somethingB
when 0..Inf
do somethingC
end
My preferred solution follows. Here the order matters and you have to repeat the myvar
, but it's much harder to leave out cases, you don't have to repeat each bound twice, and the strictness (<
vs <=
rather than ..
vs ...
) is much more obvious.
if myvar <= -5
# less than -5
elsif myvar <= -3
# between -5 and -3
elsif myvar <= -1
# between -3 and -1
elsif myvar <= 0
# between -1 and 0
else
# larger than 0
end