#### Topic: Simple formulas - alternative to eval?

We have a Rails 2.2 app that collects responses to a set of questions. Each set of responses receives a score in several different categories, and we have a formula to calculate the value for each one.

The formula typically looks like something like this:

(

(42 - ((questions[12345].response == 44444 ? 2 : 0)

+ (questions[12346].response == 55555 ? 2 : 0).to_f / 14)).to_f

) / 2

We build up the formulas into a massive string, and then evaluate it using instance_eval:

formulas.each do |formula|

eval_string += "results[#{formula.id.to_s}] = (#{formula.formula})\n"

endinstance_eval(eval_string)

This works, but is slow and memory intensive. Can anyone think of another way we could do this?

*Last edited by cdr (2009-02-24 08:03:40)*