I have the following code with a small bug in it, the case statement returns the value "other" even though the first "when" statement evaluates true and should return "boats".
I’ve been been looking at this for ages, must be something small.
CATEGORY_CLASSES = {:boats => [1, 2, 3, 4, 5, 6],
:houses => [7, 8, 9, 10],
:other => [11,12,13,14,15,16]
}
category_id = 1
category = case category_id
when CATEGORY_CLASSES[:boats].include?(category_id); "boats"
when CATEGORY_CLASSES[:houses].include?(category_id); "houses"
else "other"
end
Thanks!
A
casestatement is usually just a shorthand for anifstatement. Yours can be rewritten as:When you look at it in this form, you should see the problem clearly; while
include?returns a boolean, you’re comparing it against an integer value.