I'm trying to take a string which is a simple math expression, remove all spaces, remove all duplicate operators, convert to single digit numbers, and then evaluate.
For example, an string like "2 7+*3*95" should be converted to "2+3*9" and then evaluated as 29.
Here's what I have so far:
expression.slice!(/ /) # Remove whitespace
expression.slice!(/\A([\+\-\*\/]+)/) # Remove operators from the beginning
expression.squeeze!("0123456789") # Single digit numbers (doesn't work)
expression.squeeze!("+-*/") # Removes duplicate operators (doesn't work)
expression.slice!(/([\+\-\*\/]+)\Z/) # Removes operators from the end
puts eval expression
Unfortunately this doesn't make single digit numbers nor remove duplicate operators quite like I expected. Any ideas?