I am trying to think of a more elegant way to pass elements of an array as individual arguments to a method.
So far, I have the following code:
def fetch_data(keywords)
Product.where("name ILIKE ? AND type ILIKE ?", *construct_keywords(keywords))
end
def construct_keywords(keywords)
keywords.map{ |keyword| "%#{keyword}%" }
end
This works just fine, and I know that the splat operator is doing a good job but I want to see if instead of using that ugly asterisk, it is possible to reshape the return value of construct_keywords
in a way so that the fetch_data method will recognize the array elements as individual arguments. So ultimately I want it to be like:
def fetch_data(keywords)
Product.where("name ILIKE ? AND type ILIKE ?", construct_keywords(keywords))
end
def construct_keywords(keywords)
keywords.map{ |keyword| "%#{keyword}%" }
# reshape above array in a way so that fetch_data will recognize each elements as arguments
end
Thank you in advance.