'ruby multiple string match with [duplicate]
i've written a few lines of code like this :
if( (user_input.include?('string_a') ||
(user_input.include? ('string_b')) ||
(user_input.include?('string_c')) )
&&
user_input.include?('string_d_keyword'))
....
end # if
is there any function which can simplify the "multiple or string match" by taking multiple arguments and look like this ?
if( (user_input.multi_include_or?('string_a','string_b','string_c'))
&& (user_input.include?('string_d_keyword')))
.....
end # if
i hope to do these all in a single line and so i've leave out the option of "case when".
Thanks~
Solution 1:[1]
Use an array and any?
> user_input = "string_a"
=> "string_a"
> ["asd","string_a"].any? {|a| user_input.include? a}
=> true
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Coolness |
