Ruby 从另一个数组中排除字符串数组元素
我有两个字符串数组,需要从包含第二个字符串的数组中排除元素Ruby 从另一个数组中排除字符串数组元素,ruby,arrays,Ruby,Arrays,我有两个字符串数组,需要从包含第二个字符串的数组中排除元素 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"] 如果这是完全匹配的,我可以使用
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函数
谢谢 或
strings.reject&Regexp.union(名词).method(:match).to_proc
。并不是说你的..或strings.reject&Regexp.union(名词).method(:match).to_proc
。并不是说你的也不是更好。。
strings.reject { |string| string =~ Regexp.union(nouns) }