Ruby Koan 151提出异常

Ell*_*iot 45 ruby geometry

我正在经历红宝石公案,我在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
Run Code Online (Sandbox Code Playgroud)

然后在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
Run Code Online (Sandbox Code Playgroud)

我很困惑 - 任何帮助都会非常感激!

编辑:要完成这个公案,我需要在TriangleError类中添加一些内容 - 但我不知道是什么

更新:这是koan业力的事情:

<TriangleError> exception expected but none was thrown.
Run Code Online (Sandbox Code Playgroud)

cHa*_*Hao 52

  1. 三角形不应具有长度为0的任何边.如果是,则它可以是线段或点,具体取决于边数是多少.
  2. 负长度没有意义.
  3. 三角形的任何两边应该加起来超过第三边.
  4. 见3,重点是"更多".

您不需要更改TriangleError代码AFAICS.看起来你的语法有点古怪.尝试改变

raise new.TriangleError
Run Code Online (Sandbox Code Playgroud)

raise TriangleError, "why the exception happened"
Run Code Online (Sandbox Code Playgroud)

此外,您应该在对它们执行任何操作之前测试值(并抛出异常).将异常内容移动到函数的开头.

  • 代码本身是否说"不需要更改此代码"?它应该工作. (3认同)

Ser*_*gey 30

当a,b或c为负时,你忘记了这种情况:

def triangle(a, b, c)
  raise TriangleError if [a,b,c].min <= 0
  x, y, z = [a,b,c].sort
  raise TriangleError if x + y <= z
  [:equilateral,:isosceles,:scalene].fetch([a,b,c].uniq.size - 1)
end
Run Code Online (Sandbox Code Playgroud)

  • 简洁.但我不确定这种简洁性是否能弥补牺牲的可读性(特别是对于稍后会阅读此代码的人). (6认同)

Leo*_*tov 13

结束这样做:

def triangle(a, b, c)
  a, b, c = [a, b, c].sort
  raise TriangleError if a <= 0 || a + b <= c
  [nil, :equilateral, :isosceles, :scalene][[a, b, c].uniq.size]
end
Run Code Online (Sandbox Code Playgroud)

感谢这里的评论者:)


Mat*_*lly 9

我喜欢Cory的回答.但是我想知道,如果你有两个测试,有什么理由或任何东西可以获得:

raise TriangleError, "Sides must by numbers greater than zero" if (a <= 0) || (b <= 0) || (c <= 0)
raise TriangleError, "No two sides can add to be less than or equal to the other side" if (a+b <= c) || (a+c <= b) || (b+c <= a)
Run Code Online (Sandbox Code Playgroud)


tom*_*eld 7

您无需修改​​异常.这样的事情应该有效;

def triangle(*args)
  args.sort!
  raise TriangleError if args[0] + args[1] <= args[2] || args[0] <= 0
  [nil, :equilateral, :isosceles, :scalene][args.uniq.length]
end
Run Code Online (Sandbox Code Playgroud)


Jos*_*osh 6

def triangle(a, b, c)
  [a, b, c].permutation do |sides|
    raise TriangleError unless sides[0] + sides[1] > sides[2]
  end
  case [a,b,c].uniq.size
    when 3; :scalene
    when 2; :isosceles
    when 1; :equilateral
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 如此美丽的代码! (2认同)

Ben*_*ell 5

我想要一种能够有效解析所有参数的方法,而不是依赖于测试断言中给出的顺序。

def triangle(a, b, c)
  # WRITE THIS CODE
  [a,b,c].permutation { |p| 
     if p[0] + p[1] <= p[2]
       raise TriangleError, "Two sides of a triangle must be greater than the remaining side."
     elsif p.count { |x| x <= 0} > 0
       raise TriangleError, "A triangle cannot have sides of zero or less length."
     end
  }

  if [a,b,c].uniq.count == 1
    return :equilateral
  elsif [a,b,c].uniq.count == 2
    return :isosceles
  elsif [a,b,c].uniq.count == 3
    return :scalene
  end
end
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助其他人认识到剥猫皮的方法不止一种。