I am trying to take a string that is a simple mathematical expression, removes all spaces, removes all duplicate operators, converts them to single-digit numbers, and then evaluates.
For example, a string like "2 7 + * 3 * 95" should be converted to "2 + 3 * 9" and then evaluated as 29.
Here is what I still have:
expression.slice!(/ /)
expression.slice!(/\A([\+\-\*\/]+)/)
expression.squeeze!("0123456789")
expression.squeeze!("+-*/")
expression.slice!(/([\+\-\*\/]+)\Z/)
puts eval expression
Unfortunately, this does not make single digits and does not remove duplicate operators, as I expected. Any ideas?
source
share