Multiple Inequalities in Ruby

人走茶凉 提交于 2019-12-19 10:45:11

问题


Complete ruby noob.

I have a value which I want to check against several numbers (1,4,7) and if it equals any of them, return false.

right now I'm using:

if $my_variable_class[1] != 1 && $my_variable_class[1] != 4 && $my_variable_class[1] != 7  

I've got a shit-ton of numbers to check for inequality against. Is there a more efficient way to handle this?


回答1:


Maybe unless [1,4,7].include?($my_variable[1]) will do the trick?




回答2:


unless [1,4,7].include? $my_variable[1]
  do_smth
end


来源:https://stackoverflow.com/questions/5257379/multiple-inequalities-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!