有没有办法通过字符串操作创建条件。这是我尝试将字符串作为条件传递到 include? strong>方法的代码。
"hello eveyone in the house".include?(%w('hello' 'world' 'new\ york' 'place').join(" || ").to_s)
答案 0 :(得分:4)
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('|')}/
)
答案 1 :(得分:2)
另一种可能性是split单词并使用set intersection:
sentence = "hello everyone in the house"
whitelist = %w(hello world new-york place)
found_words = sentence.split & whitelist
p found_words # => ["hello"]
p found_words.empty? # => false
警告:仅当白名单不包含任何包含多个字词的字符串时才有效。例如原始问题new york
。
有关更强大的解决方案,请参阅@ spickermann的回答。
答案 2 :(得分:1)
测试速度的一些基准测试和令我惊讶的是,spickerman的第一个解决方案是迄今为止在Windows机器人盒中的MRI Ruby 2.3.0中最快的,其次是交叉点,而我自己的Regexp.union解决方案最后结束: (
require 'benchmark'
s = "hello everyone in the house"
a = ['hello', 'world', 'new york', 'place']
N = 10_000
Benchmark.bmbm do |x|
x.report("Union ") { N.times { s.match(Regexp.union(a)) }}
x.report("Union and scan") { N.times { s.scan(Regexp.union(a)) }}
x.report("Interpollation") { N.times { s.match(/#{a.join('|')}/)}}
x.report("intersect ") { N.times { s.split & a }}
x.report("block ") { N.times { a.any? { |word| s.include?(word)}}}
end
Rehearsal --------------------------------------------------
Union 0.110000 0.000000 0.110000 ( 0.116051)
Union and scan 0.124000 0.000000 0.124000 ( 0.121038)
Interpollation 0.110000 0.000000 0.110000 ( 0.105943)
intersect 0.015000 0.000000 0.015000 ( 0.018456)
block 0.000000 0.000000 0.000000 ( 0.001908)
----------------------------------------- total: 0.359000sec
user system total real
Union 0.109000 0.000000 0.109000 ( 0.111704)
Union and scan 0.125000 0.000000 0.125000 ( 0.119122)
Interpollation 0.109000 0.000000 0.109000 ( 0.105288)
intersect 0.016000 0.000000 0.016000 ( 0.017283)
block 0.000000 0.000000 0.000000 ( 0.001764)