Answer the question
In order to leave comments, you need to log in
How to use formula template from db in ruby on rails?
In short, there is a formula in the database, for example "PRICE * 0.07". The template is stored as text. I need this formula to work out: first, PRICE should be replaced by a number that is taken from the variable and then multiplied by 0.07. accordingly, you need to be able to write any actions (multiply, subtract, divide)
Answer the question
In order to leave comments, you need to log in
Solved such a problem with the help of an excellent library: https://github.com/rubysolo/dentaku
Able to parse mathematical and logical formulas, coding dynamic values of variables.
You can write an interpreter that executes your formulas. The easiest way to do this is to master some kind of parsing library. On ruby, I used treetop and citrus, the latter will work for you.
Take a look at the example for citrus, it just parses such an interpreter of the simplest expressions.
some_variable = 10
string_from_database = "some_variable * 5"
eval(string_from_database)
=> 50
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question