Ruby: map tags into a boolean condition to get a true/false result

Posted by cgyDeveloper on Stack Overflow See other posts from Stack Overflow or by cgyDeveloper
Published on 2010-04-08T21:18:00Z Indexed on 2010/04/08 21:23 UTC
Read the original article Hit count: 162

Filed under:

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?

© Stack Overflow or respective owner

Related posts about ruby