【问题标题】:Ruby Koans class or module required需要 Ruby Koans 类或模块
【发布时间】:2015-02-18 17:27:35
【问题描述】:

我在 Ruby Koans 中收到以下错误:

AboutHashes#test_accessing_hashes_with_fetch 破坏了你的业力。

师父说: 你还没有达到觉悟。 我感到沮丧。不要害怕寻求帮助。

您寻求的答案... 需要类或模块

请仔细思考以下代码: /home/s/Downloads/github/rubykoans/about_hashes.rb:26:in `test_accessing_hashes_with_fetch'

有问题的行是以下方法的一部分:

def test_accessing_hashes_with_fetch
  hash = { :one => "uno" }
  assert_equal "uno", hash.fetch(:one)
  assert_raise(nil) do
    hash.fetch(:doesnt_exist)
  end

如您所见,它要求一个类或模块,所以我很困惑,因为这不是我以前在 Koans 中遇到过的错误。

【问题讨论】:

    标签: ruby


    【解决方案1】:

    问题出在以下几行:

    assert_raise(nil) do
      hash.fetch(:doesnt_exist)
    end
    

    assert_raise 测试宏需要一个异常类作为其参数。你提供了零。

    要跳到答案,使用哈希中不存在的键对哈希调用 fetch 将出现 raise KeyError 异常。所以测试应该是这样的:

    assert_raise(KeyError) do
      hash.fetch(:doesnt_exist)
    end
    

    【讨论】:

    • 谢谢!这正是问题所在。
    猜你喜欢
    • 2015-03-15
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    相关资源
    最近更新 更多