5

有什么明智的说法。

if @thing == "01" or "02" or "03" or "04" or "05"

(数字包含在一列数据类型字符串中。)

4

2 回答 2

11

制作一个数组并使用.include?

if ["01","02","03","04","05"].include?(@thing)

如果值真的都是连续的,你可以使用像(1..5).include? For strings 这样的范围,你可以使用:

if ("01".."05").include?(@thing)
于 2012-04-21T14:58:41.087 回答
3

或者使用 case 语句:

case @thing
when "01", "02", "03", "04", "05"
  # do your things
end

这种方法的两种变体:

case @thing
when "01".."05"
  # do your things
end

case @thing
when *%w[01 02 03 04 05]
  # do your things
end

因为caseuses ===,你也可以这样写:("01".."05") === @thing

于 2012-04-21T15:05:57.583 回答