我在您的代码片段中注意到两件事:
您在ruby中混合了两种类型的switch-case语句。
case a
when 1..5
"It's between 1 and 5"
when 6
"It's 6"
end
case
when a > 1 && a < 5
"It's between 1 and 5"
when a == 6
"It's 6"
end
看到一个区别,在第一种情况下,当您直接比较案例时,您必须提到您要比较的变量名称,而在第二种情况下,您要与true比较&通过显式地在变量上放置条件,使其不需要放在case旁边,从而使条件为false。
第二件事是根据您的条件声明,您需要在case声明中使用else case。
red =
case
when 6 * temp_r < 1
temp_2 + (temp_1 - temp_2) * 6 * temp_r
when 2 * temp_r < 1
temp_1
when 3 * temp_r < 2
temp_2 + (temp_1 - temp_2) * (0.666 - temp_r * 6)
else
temp_2
end