开发者

is there a nicer way to phrase the following logical expression in Ruby? [duplicate]

开发者 https://www.devze.com 2023-02-15 20:43 出处:网络
This question already has answers here: 开发者_运维知识库 Closed 11 years ago. Possible Duplicate:
This question already has answers here: 开发者_运维知识库 Closed 11 years ago.

Possible Duplicate:

Multiple Inequalities in Ruby

Hi All, I have an ugly logical expression, and I just know there's a much nicer, more concise way to phrase this in ruby:

some_variable == 1 || some_variable == 2 || some_variable == 4

All suggestions welcomed, Thanks


[1,2,4].include? some_variable


a = [1,3,4]
a.indclude?(some_variable)


If you want to do something conditionally, you can also do:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号