2012-03-10 44 views
0

我试图通过使用从this website下载的代码来学习ruby。这里应该等于什么?

我被困在这一点。

def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil 
    # What happens when you call a method that doesn't exist. The 
    # following begin/rescue/end code block captures the exception and 
    # makes some assertions about it. 
    begin 
     nil.some_method_nil_doesnt_know_about 
    rescue Exception => ex 
     # What exception has been caught? 
     assert_equal NoMethodError, ex.class 

    # What message was attached to the exception? 
    # (HINT: replace __ with part of the error message.) 
    assert_match(/__/, ex.message) 
end 

我应该用错误消息的一部分,以取代__,但我都没有成功。好吧,我是,因为经过几次尝试,我用一个空格替换了它,因为我发现错误消息在单词之间有空格。但是我怎么看这个错误信息呢?

+0

就在您的例子并在我的控制台NoMethodError工作。你可以发布错误消息吗? – alony 2012-03-10 17:51:58

+0

我认为我发布我的问题时犯了一个错误,所以我更新了它。 – 2012-03-10 19:55:37

回答

6

你会得到这里NoMethodError:

>> def tst 
>> nil.an_unknown_meth 
>> rescue Exception => ex 
>> puts ex.class 
>> puts ex.message 
>> end 
=> nil 

>> tst 
NoMethodError 
undefined method `an_unknown_meth' for nil:NilClass 

所以NoMethodError一类,并/undefined method .* for nil:NilClass/作为消息应该适合。

更多信息关于NoMethodErrorgenerally on Ruby Exceptions在红宝石文档