stack = Array.new while true printf "Enter next value/command: " str = gets.chomp! begin if str == "+" if stack.length < 2 raise "Not enough on stack!" end op1 = stack.pop op2 = stack.pop stack.push(op1 + op2) elsif str == "-" if stack.length < 2 raise "Not enough on stack!" end op1 = stack.pop op2 = stack.pop stack.push(op1 - op2) elsif str == "*" if stack.length < 2 raise "Not enough on stack!" end op1 = stack.pop op2 = stack.pop stack.push(op1 * op2) elsif str == "/" if stack.length < 2 raise "Not enough on stack!" end op1 = stack.pop op2 = stack.pop stack.push(op1 / op2) elsif str == "^" if stack.length < 2 raise "Not enough on stack!" end op1 = stack.pop op2 = stack.pop stack.push(pow (op1, op2)) elsif str == "i" printf "There is a total of %d items on the stack.\n", stack.length print "[" stack.each {|x| print x, ", " } print "]\n" elsif str[0,1] == "p" stack.pop elsif str[0,1] == "q" exit else stack.push(Float(str)) end rescue RuntimeError => error puts error rescue ArgumentError puts "You entered an invalid command. Ignoring." end end