1

I have two arrays of strings, and I need to exclude elements from one that contain elements of the second.

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

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

the ideal result would be an array (either new or modified strings) that contains

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

If this were exact matches, I could use the built-in functions:

result = strings - nouns

but obviously that won't work here.

Is there a simple way to do this, using grep, select, or some other ruby function?

Thanks!

0

1 Answer 1

4
strings.reject { |string| string =~ Regexp.union(nouns) }
Sign up to request clarification or add additional context in comments.

1 Comment

or strings.reject &Regexp.union(nouns).method(:match).to_proc. Not that yours isn't much better..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.