当我尝试完成作为 test-first 的 Learn Ruby 材料一部分的 RPN 计算器作业时,我不断收到错误消息。当我进行以下 rspec 测试时,我得到“nil can't be coerced into fixednum”并且无法得到“Calculator is empty”错误。任何帮助将非常感激。
it "fails informatively when there's not enough values stacked away" do
expect {
calculator.plus
}.to raise_error("calculator is empty")
class RPNCalculator
attr_accessor :stack
def initialize
@stack = [0]
end
def push(x)
@stack.push(x)
end
def plus
@stack.push(@stack.pop + @stack.pop)
end
def minus
@stack.push(-@stack.pop + @stack.pop)
end
def divide
denom = @stack.pop
@stack.push(@stack.pop.to_f / denom)
end
def times
@stack.push(@stack.pop * @stack.pop)
end
def value
@stack.last
end
def pop(x)
value = @stack.pop(x)
raise "calculator is empty" if @stack.nil?
return value
end
end