I would like to understand a code that creates a RPN calculator:
class RPNCalculator
def evaluate(expression)
operators = ["+", "-", "*"]
stack = []
array = expression.split(" ")
array.each do |i|
if operators.include?(i)
second_operand = stack.pop
first_operand = stack.pop
stack.push(first_operand.send(i, second_operand))
else
stack.push(i.to_i)
end
end
stack.pop
end
end
I do not understand these lines in particular:
if operators.include?(i)
second_operand = stack.pop
first_operand = stack.pop
stack.push(first_operand.send(i, second_operand))
else
stack.push(i.to_i)
If someone could give me a full run down of the code, it would be very helpful.