-3

I am having saving dynamic mathematical formula as string. I have two predefined functions like avg(A,B) and best_of(1,A,B).The formula can consist of any mathematical operator, with nested parenthesis.

How do I execute the formula in ruby. The following are few formula formats,

avg(A,B,C)+D

best_of(1,A,B,C)+avg(D,E)

avg(best_of(1,A,B)+C+D)+E 
Ankur Alankar Biswal
  • 1,184
  • 1
  • 12
  • 23

1 Answers1

0

You can write a parser for your expressions. Take a look at Treetop or Parslet.

Or take a look at an existing math expression parser like Dentaku where you can supply custom functions.

Pascal
  • 8,464
  • 1
  • 20
  • 31