2012-03-10 2 views
0

this website에서 다운로드 한 코드를 사용하여 루비를 배우려고합니다.여기에서 ex.class는 어떻게되어야합니까?

나는이 시점에서 고생했다.

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

+0

질문을 올렸을 때 실수를 저질렀다고 생각합니다. –

답변

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/을. 루비 문서에

NoMethodError

더 많은 정보와 generally on Ruby Exceptions

관련 문제