【发布时间】:2015-01-06 10:19:28
【问题描述】:
一直在研究 Ruby koans,我达到了 262。我已解决如下:
def test_catching_messages_makes_respond_to_lie
catcher = AllMessageCatcher.new
assert_nothing_raised do
catcher.any_method
end
assert_equal false, catcher.respond_to?(:any_method)
end
...但我不知道这段代码在做什么。我查看了 assert_nothing_raised,但文档的解释非常稀疏和深奥。我知道这节课应该教我respond_to?在某些情况下“撒谎”,但这里的情况是什么?
:any_method 不存在吗?如果它在 assert_nothing_raised 的块中定义,它是否不存在?简而言之,这段代码到底发生了什么?
谢谢。
编辑
这是 WellBehavedFooCatcher 类:
class WellBehavedFooCatcher
def method_missing(method_name, *args, &block)
if method_name.to_s[0,3] == "foo"
"Foo to you too"
else
super(method_name, *args, &block)
end
end
end
【问题讨论】:
标签: ruby