为什么这不起作用 - 红宝石koans

时间:2012-09-23 08:27:42

标签: ruby

我提出了以下解决方案,以获得公案。

# and
#   about_triangle_project_2.rb
#
def triangle(a, b, c)

   driehoek = Array.new[ a, b, c].sort

  raise (TriangleError), "length cannnot be 0 or lesser" if (driehoek[0] <= 0) 
  raise (TriangleError), "impossible triangle" if (driehoek[0] + driehoek[1] < driehoek[2])

 return :equilateral if ((a == b) and (b == c))
 return :isosceles if (((a == b) and (b != c)) or 
                   ((a != b) and (b == c)) or 
                   ((a == c) and (a != b)))
  return :scalene  if ((a !=b) and (b != c))
end

# Error class used in part 2.  No need to change this code.
class TriangleError < StandardError
end

但是现在当使用三角形[2,2,2]时,我看到了以下错误消息:

The answers you seek...
 wrong number of arguments (3 for 2)

Please meditate on the following code:
  ./triangle.rb:18:in `[]'
  ./triangle.rb:18:in `triangle'

谁能告诉我这里有什么问题?

鲁洛夫

1 个答案:

答案 0 :(得分:1)

问题在于您如何创建阵列。 new是一种方法,您需要使用parens ()。改变这个:

driehoek = Array.new[ a, b, c].sort

对此,它应该工作:

driehoek = Array.new([a, b, c]).sort