Bit of an odd question but I want to do something like:
a = 6
b = 12
cond = "((a > 5 and b > 10) or a > 50)" #user generated
if cond
#do something
end
How can I parse the string, substitute the variables and run as a conditional.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Once you have a Ruby string
cond, you can just doeval(cond). But be aware that people will criticize this for security reasons.By the way, if you actually do
eval(cond)to your code, it will return an error because the stringcondin your example is not a valid Ruby code.