I have an array of tags per item like so:
item1 = ['new', 'expensive']
item2 = ['expensive', 'lame']
I also have a boolean expression as a string based on possible tags:
buy_it = "(new || expensive) && !lame"
How can I determine if an item matches the buying criteria based on the tags associated with it? My original thought was to do a gsub on all words in buy_it to become ‘true’ or ‘false’ based on them existing in the itemx tags array and then exec the resulting string to get a boolean result.
But since the Ruby community is usually more creative than me, is there a better solution?
EDIT:
Just to clarify, buy_it in my example is dynamic, users can change the criteria for buying something at run-time.
Along the lines of your
gsubidea, instead of substituting each word for true/false every time, why not substitute each “query” into an expression that can be re-used, e.g. the examplebuy_it:It could be evaluated into a
Procand used like this:Of course, care must be taken that the expression does not contain malicious code. (One solution might be to strip all but the allowed operator characters from the string and of course be wary of exceptions during eval.)