Ruby find like string array from an array

hudadiaz

Say that I have three arrays arr1, arr1 and arr3.

arr1 = ["apple", "book", "car", "dog"]
arr2 = ["apple", "book"]
arr3 = ["app", "boo"]

How can I check if arr1 includes: arr2, and arr3 with like wildcards.

Stefan

You could use Enumerable#grep:

arr3.all? { |item| arr2.grep(/#{item}/)[0] }
#=> true

This matches substrings (e.g. ook), to match only prefixes use /^#{item}/.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related