如果我对此代码运行下面的测试,它会返回
ALERT: an event that always happens
我希望它也能放
ALERT: an event that never happens
但事实并非如此。我认为差异的原因是各自测试中的“真”和“假”,但我不明白为什么“真”或“假”在这种情况下会有所不同。方法“事件”说
puts "ALERT: #{name}" if yield
如果测试的结果可以解释为“真”在这种情况下等于“产量”,而“假”则不然,那么“假”如何否定“产量”?
问题:'if yield' 是否意味着'yield if block 评估为真'?
代码
def event(name)
puts "ALERT: #{name}" if yield
end
Dir.glob('*events.rb').each {|file| load file }
测试
event "an event that always happens" do
true
end
event "an event that never happens" do
false
end
输出
ALERT: an event that always happens