You're not comparing apples to apples.
in "if a", a is a statement that returns true or false.
in "when a", a is the value of the variable 's'
your comparison should look like...
if s == 0
# do this
elseif s == 1
# do this
elseif s == 2
# do this
else
# do the default action
end
compared to:
case s
when 0
# do this
when 1
# do this
when 2
# do this
else
# do the default action
end
in case, you can also use a range. "when 2..5", whereas with 'if' you would need
if s >= 2 && s <= 5.
With 'case' you are testing every expression to see if it is equal (===) to 's'
with 'if', each test can be a completely different comparison.
if @face == "ugly"
# flip her over
elseif @finger.is_in(nose)
# tell her your name is Treg
end
We could probably do a benchmark with a incrementing loop to see which is more efficient, but I expect the results would be so close, that it just wouldn't matter.
Your decision should be based on your application. neither is 'slow'.
Be Well