nod*_*nja 0 ruby methods error-handling exception-handling exception
我正在尝试使用从本网站下载的代码来学习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
Run Code Online (Sandbox Code Playgroud)
结束
我应该用错误消息的一部分替换__,但我没有成功.好吧,我是,因为经过几次尝试后我只是用空格替换它,因为我认为错误信息在单词之间有空格.但我怎么看错误信息是什么?
你会在这里得到一个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
Run Code Online (Sandbox Code Playgroud)
所以NoMethodError对于一个类,并且/undefined method .* for nil:NilClass/作为一个消息应该适合.
有关NoMethodError的更多信息以及ruby-docs中的Ruby Exceptions