有没有办法通过字符串操作创建条件.这里是我尝试将字符串作为条件传递给include的代码?方法. "hello eveyone in the house".include?(%w('hello' 'world' 'new\ york' 'place').join(" || ").to_s) 包含条件的参
"hello eveyone in the house".include?(%w('hello' 'world' 'new\ york' 'place').join(" || ").to_s)
包含条件的参数?是不可能的,因为包括?只接受一个字符串参数.
但你可以这样写:
['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('|')}/
)
