优雅的链式“或"用于在 Ruby 中对同一变量进行测试 [英] Elegant chained 'or's for tests on same variable in Ruby
本文介绍了优雅的链式“或"用于在 Ruby 中对同一变量进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这样说的合理方式是什么.
What's the sensible way of saying this.
if @thing == "01" or "02" or "03" or "04" or "05"
(数字包含在一列数据类型字符串中.)
(The numbers are contained in a column of datatype string.)
推荐答案
制作数组并使用 .include?
if ["01","02","03","04","05"].include?(@thing)
如果真的都是连续的,可以使用(1..5).include?
这样的范围,对于字符串,可以使用:
If the values really are all consecutive, you can use a range like (1..5).include?
For strings, you can use:
if ("01".."05").include?(@thing)
这篇关于优雅的链式“或"用于在 Ruby 中对同一变量进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文