有没有办法通过字符串操作来创建条件。这是我尝试将字符串作为条件传递给 include? 方法的代码。
"hello eveyone in the house".include?(%w('hello' 'world' 'new\ york' 'place').join(" || ").to_s)
最佳答案
include?
的条件参数是不可能的,因为 include?
只接受字符串参数。
但是你可以这样写:
['hello', 'world', 'new york', 'place'].any? { |word|
"hello everyone in the house".include?(word)
}
或者您可以从您的字符串生成一个正则表达式:
"hello eveyone in the house".match?(
/#{['hello', 'world', 'new york', 'place'].join('|')}/
)
关于ruby-on-rails - 将字符串作为 ruby 中方法的条件传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41352497/