2012-03-10 13 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 

エンド

Iは、エラーメッセージの一部と__を交換することになってんだけど、私は成功していません。さて、私は、エラーメッセージに単語間にスペースがあったと思ったので、いくつか試行した後、スペースで置き換えたので、私はそうでした。しかし、どのようにエラーメッセージが表示されるのですか?

+0

は、右のあなたの例では、私のコンソールでNoMethodErrorで動作ありません。あなたはエラーメッセージを投稿できますか? – alony

+0

私は質問を投稿したときに間違いを犯したと思いますので、私はそれを更新しました。 –

答えて

6

あなたはここにNoMethodErrorを取得します:クラスのためにそう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 

を、そしてメッセージなど/undefined method .* for nil:NilClass/は収まるはずです。ルビー・ドキュメントで

NoMethodError

詳細情報とgenerally on Ruby Exceptions

関連する問題