A
A
Alexander Petrov2018-04-13 15:41:09
Ruby on Rails
Alexander Petrov, 2018-04-13 15:41:09

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

4 answer(s)
K
Kirill Platonov, 2018-04-17
@kirillplatonov

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.

A
Artur Bordenyuk, 2018-04-13
@HighQuality

How did you try to solve it? What are the difficulties?

V
vsuhachev, 2018-04-16
@vsuhachev

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.

B
blackst0ne, 2018-05-21
@blackst0ne

some_variable = 10 
string_from_database = "some_variable * 5"

eval(string_from_database)
=> 50

The biggest problem here is to properly sanitize the string so that you don't slip some dangerous construct there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question