Your question is ambiguous.
If you count occurrences, then:
('This is a test string'.scan(/\w+/).map(&:downcase) & ['test', 'is']).length
If you are counting tokens, then:
(['test', 'is'] & 'This is a test string'.scan(/\w+/).map(&:downcase)).length
You can speed up the calculation by replacing Array#& with some operation using Hash (or Set ).
source share