1

我有两个字符串数组,我需要从一个包含第二个元素的元素中排除元素。

strings = ["chairs are on sale today", "my dog likes bumblebees", "one bad apple", "most snow is green"]

nouns = ["chair", "stove", "apple"]

理想的结果将是一个数组(新的或修改的字符串),其中包含

["my dog likes bumblebees", "most snow is green"]

如果这是完全匹配,我可以使用内置函数:

result = strings - nouns

但显然这在这里行不通。

有没有一种简单的方法可以做到这一点,使用 grep、select 或其他一些 ruby​​ 函数?

谢谢!

4

1 回答 1

4
strings.reject { |string| string =~ Regexp.union(nouns) }
于 2013-10-02T21:20:47.897 回答