在 ruby 中将字符串作为方法的条件传递

passing string as a condition of a method in ruby

有没有办法通过字符串操作来创建条件。这是我试图将字符串作为条件传递给 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('|')}/
)

另一种可能性是 split the words and use 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 的回答。

做了一些基准测试来测试速度,令我惊讶的是,spickerman 的第一个解决方案是迄今为止 MRI Ruby 2.3.0 在 windows 盒子上最快的解决方案,其次是交叉点,而我的自己的 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)