【问题标题】:is there a nicer way to phrase the following logical expression in Ruby? [duplicate]有没有更好的方法来表达 Ruby 中的以下逻辑表达式? [复制]
【发布时间】:2011-03-10 15:07:49
【问题描述】:

可能重复:
Multiple Inequalities in Ruby

大家好,我有一个丑陋的逻辑表达式,我只知道在 ruby​​ 中有一种更好、更简洁的表达方式:

some_variable == 1 ||一些变量 == 2 || some_variable == 4

欢迎所有建议,谢谢

【问题讨论】:

标签: ruby


【解决方案1】:
[1,2,4].include? some_variable

【讨论】:

    【解决方案2】:
    a = [1,3,4]
    a.indclude?(some_variable)
    

    【讨论】:

      【解决方案3】:

      如果你想有条件地做某事,你也可以这样做:

      case some_variable
      when 1, 2, 4
          blahblah
      else
          blahblah
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-22
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-19
        相关资源
        最近更新 更多