开发者

is there a nicer way to phrase the following logical expression in Ruby? [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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜