Home > Software engineering >  ROR Trying to compare string with array of string
ROR Trying to compare string with array of string

Time:01-06

For example:

array = ['text_first', 'text_second', 'text_third']

Now trying to compare with 'text_first_file', 'text_first_2023' or 'text11_first'

Trying to use regex - but failing.

UseCase:

array includes 'text_first_file' => TRUE
array includes 'text_first_2023' => TRUE
array includes 'text_first-2023' => TRUE
array includes 'text11_first'    => FALSE

CodePudding user response:

You don't need the regular expression

array = %w[text_first text_second text_third]
string = 'text_first_file'
p array.map { |x| string.include? x }.any?

Output

true
  • Related