首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >帮助Ruby Koans #6 -捕获了什么异常?

帮助Ruby Koans #6 -捕获了什么异常?
EN

Stack Overflow用户
提问于 2010-09-29 11:41:59
回答 11查看 8.6K关注 0票数 10

我正试着通过Koans学习Ruby,但是我被困在了第六步。

代码如下:

代码语言:javascript
复制
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 some assertions about it.  

  begin
    nil.some_method_nil_doesnt_know_about
  rescue Exception => ex
    # What exception has been caught?
    assert_equal __, ex.class

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

我知道我应该用与错误消息"NoMethodError“有关的东西来替换__,但我似乎搞不清楚。

这是我在运行“path_to_enlightenment.rb”时收到的错误消息:

代码语言:javascript
复制
The answers you seek...
  <"FILL ME IN"> expected but was  <NoMethodError>.

我真的很感激这方面的一些指导-它快把我逼疯了!我很想知道答案和可能的解释。谢谢!

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2010-09-29 23:06:33

这里的答案是"NoMethodError“

你需要两边的项是相等的,因此将它们都设为ex.class可以做到这一点。

然后,您需要转到下面的/__/。

票数 12
EN

Stack Overflow用户

发布于 2011-05-27 03:40:32

我必须将assert_equal语句放在括号中才能使这条语句通过。一定是个窃听器。

代码语言:javascript
复制
  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 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("undefined method", ex.message)
    end
  end
票数 4
EN

Stack Overflow用户

发布于 2012-02-20 15:27:30

需要将__替换为实际的

代码语言:javascript
复制
assert_equal NoMethodError, ex.class 
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3818561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档