我正在经历红宝石公案,我在 151,但我刚刚碰壁了。
这是公案:
# You need to write the triangle method in the file 'triangle.rb'
require 'triangle.rb'
class AboutTriangleProject2 < EdgeCase::Koan
# The first assignment did not talk about how to handle errors.
# Let's handle that part now.
def test_illegal_triangles_throw_exceptions
assert_raise(TriangleError) do triangle(0, 0, 0) end
assert_raise(TriangleError) do triangle(3, 4, -5) end
assert_raise(TriangleError) do triangle(1, 1, 3) end
assert_raise(TriangleError) do triangle(2, 4, 2) end
end
end
然后在triangle.rb中我们有:
def triangle(a, b, c)
# WRITE THIS CODE
if a==b && a==c
return :equilateral
end
if (a==b && a!=c) || (a==c && a!=b) || (b==c && b!=a)
return :isosceles
end
if a!=b && a!=c && b!=c
return :scalene
end
if a==0 && b==0 && c==0
raise new.TriangleError
end
end
# Error class used in part 2. No need to change this code.
class TriangleError < StandardError
end
我很困惑 - 任何帮助将不胜感激!
编辑:为了完成这个公案,我需要在 TriangleError 类中添加一些东西 - 但我不知道是什么
更新:以下是公案因果报应所说的:
<TriangleError> exception expected but none was thrown.
- 三角形不应有长度为 0 的边。如果有,则它是一条线段或一个点,具体取决于有多少条边为 0。
- 负长度没有意义。
- 三角形的任意两条边之和应大于第三条边。
- 参见3,重点关注“更多”。
您不需要更改 TriangleError 代码,AFAICS。看起来你的语法有点古怪。尝试改变
raise new.TriangleError
to
raise TriangleError, "why the exception happened"
此外,在对这些值执行任何操作之前,您应该测试这些值(并引发异常)。将异常内容移至函数的开头。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)