Ruby on rails Case语句不起作用时

时间:2017-02-06 14:24:51

标签: ruby

我无法理解为什么我的案例/何时陈述不起作用......我无法找到有关' AND(&&)'操作

points = -180
case points
 when (points >= -9999) && (points < -300) then
    title = "bad player"
 when (points >= -300) && (points < -100) then
    title = "not reliable"
 when (points >= -100) && (points < 100) then
    title = "Newbie"
end

我得到的标题=空白..

谢谢

2 个答案:

答案 0 :(得分:3)

试试这个

points = -180
case
when (points >= -9999) && (points < -300)
  title = "bad player"
when (points >= -300) && (points < -100)
  title = "not reliable"
when (points >= -100) && (points < 100)
  title = "Newbie"
end

#=> "not reliable"

您也可以使用范围

points = -180

title = 
  case points
  when -9999..-301
    "bad player"
  when -300..-99
    "not reliable"
  when -100..99
    "Newbie"
  end


#=> "not reliable"

答案 1 :(得分:1)

所以你是case - points变量,但你尝试在when子句中做一些完全不同的事情。三个when中的至少两个将评估为false,另一个评估为true。因此,您实际上是在检查-180true还是false

你真正想做的事情可能是:

case points
when -9999...-300 then 'bad player'
when -300...-100  then 'not reliable'
when -100...100   then 'Newbie'
end