需要Ruby Koans类或模块
我在Ruby Koans中遇到以下错误: AboutHashes#test(测试)访问(散列)和(获取)已经破坏了你的业力 大师说: 你还没有开悟。 我感到沮丧。不要害怕寻求帮助 你寻求的答案。。。 所需的类或模块 请思考以下代码: /home/s/Downloads/github/rubykoans/about_hashes.rb:26:in'test_访问_hashes_与_fetch' 该行是以下方法的一部分:需要Ruby Koans类或模块,ruby,Ruby,我在Ruby Koans中遇到以下错误: AboutHashes#test(测试)访问(散列)和(获取)已经破坏了你的业力 大师说: 你还没有开悟。 我感到沮丧。不要害怕寻求帮助 你寻求的答案。。。 所需的类或模块 请思考以下代码: /home/s/Downloads/github/rubykoans/about_hashes.rb:26:in'test_访问_hashes_与_fetch' 该行是以下方法的一部分: def 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中遇到的错误 问题出在以下几行:
assert_raise(nil) do
hash.fetch(:doesnt_exist)
end
assert\u raise
test宏期望一类异常作为其参数。你提供了零
要跳到答案,对哈希中不存在键的哈希调用fetch将引发异常。因此,测试应具有以下内容:
assert_raise(KeyError) do
hash.fetch(:doesnt_exist)
end
谢谢这正是问题所在。