每个 'when' 块中具有多个值的 Case 语句 [英] Case statement with multiple values in each 'when' block

查看:43
本文介绍了每个 'when' 块中具有多个值的 Case 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能描述我正在寻找什么的最好方法是向您展示我迄今为止尝试过的失败代码:

The best way I can describe what I'm looking for is to show you the failed code I've tried thus far:

case car
  when ['honda', 'acura'].include?(car)
    # code
  when 'toyota' || 'lexus'
    # code
end

我有大约 4 或 5 种不同的 when 情况,应该由大约 50 种不同的 car 可能值触发.有没有办法用 case 块来做到这一点,还是我应该尝试一个巨大的 if 块?

I've got about 4 or 5 different when situations that should be triggered by approximately 50 different possible values of car. Is there a way to do this with case blocks or should I try a massive if block?

推荐答案

case 语句中,, 等价于 ||if 语句中.

In a case statement, a , is the equivalent of || in an if statement.

case car
   when 'toyota', 'lexus'
      # code
end

你可以用 Ruby case 语句做的其他一些事情

这篇关于每个 'when' 块中具有多个值的 Case 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆