def calculator string while string.match /[*\/]/ # p string string.gsub!(/(\d+)\s*([*\/])\s*(\d+)/) do |regex| num1 = $1.to_i num2 = $3.to_i operator = $2.to_sym "#{[num1, num2].reduce(operator)}" end end while string.match /[-\+]/ string.gsub!(/(\d+)\s*([-\+])\s*(\d+)/) do |regex| num1 = $1.to_i num2 = $3.to_i operator = $2.to_sym "#{[num1, num2].reduce(operator)}" end end string.to_i end