Let's say I have a program like this:
class Student
attr_reader :first_name, :last_name, :subject, :color
def initialize(first_name, last_name, subject, color)
@first_name = first_name
@last_name = last_name
@subject = subject
@color = color
end
def keywords
[] << @first_name << @last_name << @subject << @color
end
end
student1 = Student.new('john', 'smith', 'math', 'blue')
student2 = Student.new('ann', 'smitten', 'english', 'blue')
students = [student1, student2]
students.select do |student|
...
end
I'm trying to accomplish the following:
1) select an array of Students that match my query with my Student.keywords array
2) my query is also an array of single words
3) if queryeven partially matches keyword it's a "match"
For example keywords for student1 are:
['john', 'smith', 'math', 'blue']
any of the following query arrays IS A MATCH
['j', 'mat'], ['it', 'blue', 'green']
any of the following query arrays IS NOT A MATCH
['johny'], ['johny', 'smithy', 'mathy', 'bluegreen']
How would I write this? I've been scratching my head for hours and no joy!
Also, I do need this to be fairly performant since I may need to iterate over 1000 or more array elements. I need a pure ruby solution as well.
['it', 'blue', 'green']is not a match, because no students have green..grep(Regexp.new(keyword))part. It filters array, leaving only those elements which contain the keyword..all?with.any?then.