Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby ex.class在这里应该等于什么?_Ruby_Exception_Methods_Exception Handling_Error Handling - Fatal编程技术网

Ruby ex.class在这里应该等于什么?

Ruby ex.class在这里应该等于什么?,ruby,exception,methods,exception-handling,error-handling,Ruby,Exception,Methods,Exception Handling,Error Handling,我正在尝试使用从下载的代码学习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 # make

我正在尝试使用从下载的代码学习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
结束


我应该用错误消息的一部分替换,但我没有成功。是的,因为在尝试了几次之后,我用一个空格替换了它,因为我发现错误消息在单词之间有空格。但是我怎么才能看到错误信息呢?

您将在这里看到一个命名错误:

>> 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.*对于nil:NilClass/
作为消息应该合适


关于ruby文档的更多信息

您将在此处获得一个命名错误:

>> 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.*对于nil:NilClass/
作为消息应该合适


关于ruby文档的更多信息

right您的示例确实可以在我的控制台中使用NoMethodError。那么你能发布错误消息吗?我想我发布问题时犯了一个错误,所以我更新了它。对,你的示例在我的控制台中与NoMethodError一起工作。你能发布错误信息吗?我想我发布问题时出错了,所以我更新了它。