I have a list of car makes
makes = [acura, honda, ford]
and I'm trying to iterate through an array of strings and find out if the individual string contains one of these makes AND if it does, to put that specific makes into an array
so I've got
strings.each do |string|
if string.include?(*makes)
else
end
end
how do I use the current argument of the splat process to determine which make matched up with the string? Is there a way to do this?
Edit: As I posted in the comments below, I'm looking for a specific make to be returned, instead of a true/false answer. So if the string is "New toyota celica", the return should be "toyota".
Using Enumerable#any?
:
makes = ['acura', 'honda', 'ford']
strings = ['hyundai acura ford', 'sports car']
strings.each do |string|
p makes.any? { |make| string.include? make }
end
Alternative that use regular expression: (See Regexp::union
)
strings = ['hyundai acura ford', 'sports car']
makes = ['acura', 'honda', 'ford']
pattern = Regexp.union(makes)
strings.each do |string|
p string.match(pattern) != nil
end
UPDATE
strings.each do |string|
p makes.find { |make| string.include? make }
end
or
strings.each do |string|
p makes.select { |make| string.include? make }
end