帮助Ruby Koans#6 - 遇到了什么异常?

时间:2010-09-29 03:41:59

标签: ruby exception

我正在尝试通过Koans学习Ruby,但我坚持第6步。

以下是代码:

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”时收到的错误消息:

The answers you seek...
  <"FILL ME IN"> expected but was  <NoMethodError>.

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

11 个答案:

答案 0 :(得分:12)

这里的答案是“NoMethodError”

你需要两边的项目,相等,因此让它们都是ex.class会做到这一点。

然后你需要继续/ __ /下面。

答案 1 :(得分:4)

我必须将assert_equal语句放入parens才能使这个语句通过。一定是个bug。

  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

答案 2 :(得分:3)

您需要将__替换为实际的

assert_equal NoMethodError, ex.class 

答案 3 :(得分:1)

Carlo Ledesma有正确的答案。 (assert_equal NoMethodError,ex.class) 不知道为什么如果有人要求你解决方程,艾略特的答案是最高评价的, 2x + 2 = 10,您不回答2x + 2 = 2x + 2。数学上你是对的,但这不是答案。

答案 4 :(得分:1)

我刚刚解决了这个问题。我一直专注于一行,但这个方法包括两个要更改的项目,一个在第16行,一个在第20行。两个都需要通过课程。

16:assert_equal NoMethodError, ex.class

20:assert_match(/*some_method_nil_doesnt_know_about*/, ex.message)

希望能帮助陷入困境的其他人!

答案 5 :(得分:1)

对于第20行,我使用了这个:assert_match(/some_method_nil_doesnt_know_about/, ex.message)我不知道那些&#34; /&#34;字符是为了。我必须研究它,但那确实有效。

我认为诀窍是,#some; some_method_nil_doesnt_know_about在邮件的单引号中,所以它正在寻找。

答案 6 :(得分:0)

我只在Koen 83上,但在大多数Koens中,您可以在“The answers you seek...”之后立即在“<"FILL ME IN"> expected but was”部分找到“答案”。我不时地打破RubyKoan框架,我会得到这个老式的ascii图形(我不确定它应该是手掌还是树,但希望你看到它时就知道了);在这些情况下,ascii图形下面的错误消息很有帮助。

答案 7 :(得分:0)

当你填写NoMethodError时,你还需要填写错误信息(“undefined method”some_methd ..“的效果)你必须用你的术语替换所有/ __ /而不仅仅是下划线

答案 8 :(得分:0)

如果删除引号,可以添加一些错误消息

assert_match(/undefined method/, ex.message)

答案 9 :(得分:0)

您需要连续回答,即从第 16 行开始,然后转到第 20 行。

line 16 => 由于 assert_equal 想要 1 = 1,我们需要查看 __,即 nil.some_method_nil_doesnt_know_aboutex.class 相同。正如错误所说;

Expected "FILL ME IN" to equal NoMethodError

我们可以看到类本身是一个“不存在的nil方法”,这意味着它不返回任何内容。所以没有必要得到错误。但是在 Ruby 中,我们有一个特定的错误,NoMethodError。解决办法是;

assert_equal NoMethodError, ex.class

line 20 => 从逻辑上讲,如果我们得到一个 Error,我们需要一个独特的消息。答案本身已经给出;

Expected "undefined method `some_method_nil_doesnt_know_about' for nil:NilClass" to match /__/

只需将该消息替换为 /__/。 这里有两个答案;

1)
assert_match(/undefined method/, ex.message)

2
assert_match(/some_method_nil_doesnt_know_about/, ex.message)

享受;p

答案 10 :(得分:0)

所以这有点令人困惑,但实际上是公案如何工作的基础 - 虽然填写 NoMethodError 是这里的有效解决方案,所以这变成了 assert_equal NoMethodError, ex.class - 它不会“解决”这个特定的问题koans 函数还有一个要修复的点(第 20 行)——只有同时解决这两个问题,才能获得成功的 koan #6