我要穿过ruby koans,我在151,我刚刚撞到了一堵砖墙.

这是koan:

# 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

然后是三角形.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

我一点也不困惑——任何帮助都将不胜感激!

编辑:为了完成这个koan,我需要在TriangleError类中添加一些东西,但我不知道是什么

更新:以下是koan karma的说法:

<TriangleError> exception expected but none was thrown.

推荐答案

  1. 三角形不应有任何长度为0的边.如果是的话,它要么是一条线段,要么是一个点,这取决于有多少边是0.
  2. 负长度没有意义.
  3. Any two sides of a triangle should add up 到 more than the third side.
  4. 参见第3条,关注"更多".

你不需要更改三角形错误代码,AFAICS.看起来你的语法有点古怪.试着改变

raise new.TriangleError

raise TriangleError, "why the exception happened"

Also, you should be testing the values (and throwing exceptions) before you do anything with them. Move the exception stuff 到 the beginning of the function.

Ruby相关问答推荐

Ruby:程序终止后仅写入/保存/可见的文件数据

删除带括号的子表达式

如何从 DateTime 值中删除区域?

构造Ruby的现代方法是什么?

在 Ruby 中导出环境变量

为什么 Ruby 中的 `a = a` `nil`?

ruby:对两个或多个数组的对应成员求和

判断文件是否包含字符串

如何在 VIM 中导航 Ruby 方法?

在 Ubuntu 上安装 Ruby 1.9.1?

将字符串与多个模式匹配

Ruby 中的抽象方法

就地修改 ruby​​ 哈希(rails strong params)

rails 控制台、RVM 和 readline 的问题

如何让 Ruby 1.9 成为 Ubuntu 上的默认 Ruby?

是否有更简单的(单行)语法来别名一个类方法?

在 Ruby 中打开默认浏览器

使用哈希值呈现 ERB 模板

通过 factory_girl 协会查找或创建记录

Ruby Activerecord IN 子句