Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Ruby进行测试_Ruby_Testing - Fatal编程技术网

用Ruby进行测试

用Ruby进行测试,ruby,testing,Ruby,Testing,请原谅这个问题的简单性。我正在学习TDD,并有以下声明 def test_equilateral_triangles_have_equal_sides assert_equal :equilateral, triangle(2, 2, 2) assert_equal :equilateral, triangle(10, 10, 10) end def test_isosceles_triangles_have_exactly_two_sides_equal assert_equ

请原谅这个问题的简单性。我正在学习TDD,并有以下声明

def test_equilateral_triangles_have_equal_sides
   assert_equal :equilateral, triangle(2, 2, 2)
   assert_equal :equilateral, triangle(10, 10, 10)
end

def test_isosceles_triangles_have_exactly_two_sides_equal
  assert_equal :isosceles, triangle(3, 4, 4)
  assert_equal :isosceles, triangle(4, 3, 4)
  assert_equal :isosceles, triangle(4, 4, 3)
  assert_equal :isosceles, triangle(10, 10, 2)
end

def test_scalene_triangles_have_no_equal_sides
  assert_equal :scalene, triangle(3, 4, 5)
  assert_equal :scalene, triangle(10, 11, 12)
  assert_equal :scalene, triangle(5, 4, 2)
end
我对这个问题提出了一个非常基本的解决方案,并希望从更有经验的程序员那里得到关于替代解决方案的反馈

我的代码:

def triangle(a, b, c)
  if (a == b) && (a == c) && (b == c)
    :equilateral
  elsif (a == b) && ((a || b) != c)
    :isosceles
  elsif (a == c) && ((a || c) != b)
    :isosceles
  elsif (b == c) && ((b || c) != a)
    :isosceles
  else
    :scalene
end

你的很多条件都是多余的。如果a==b和a==c,那么b==c是必要的。另外,如果第一个条件为false且a==b,则c不能等于它们中的任何一个,否则第一个条件将为true。因此,您可以简化为:

def triangle(a, b, c)
  if (a == b) && (a == c)
    :equilateral
  elsif (a == b) || (a == c) || (b == c)
    :isosceles
  else
    :scalene
  end
end

两条评论。首先,通常您希望测试确认肯定的情况,以及检查失败(
assert\u not\u equal

我想你可以稍微收紧一下你的
三角形
方法

考虑:如果
a==b
b==c
那么
a==c

考虑:如果
a==b
b==c
a==c
则三角形为等腰三角形


考虑:满足的第一个条件将阻止其他人执行,并排除其他情况

您可以通过对侧面进行排序来简化条件检查

sides = [a, b, c].sort
return :equilateral if sides[0] == sides[2]
return :isosceles if sides[0] == sides[1] || sides[1] == sides[2]
return :scalene
或者更简单的方法是使用
.uniq

sides = [a, b, c].uniq
type = case sides.length
        when 1 then :equilateral 
        when 2 then :isosceles 
        when 3 then :scalene
       end
这应该包括你:

def triangle(a, b, c)

  raise TriangleError, 'Sum of two sides must be greater than the third.' if a + b < c || b + c < a || a + c < b # not a triangle!
  raise TriangleError, 'Sum of two sides must not be equal to the third.' if a + b == c || b + c == a || a + c == b # degenerate triangle!

  return :equilateral  if a == b && a == c
  return :isosceles    if a == b || b == c || a == c
  return :scalene      if a != b && b != c && a != c

end
def三角形(a、b、c)
提高三角形错误,“两条边之和必须大于第三条。”如果a+b

通过Ruby Koans分享您的进步!:-)

@TomHarrisonJr抱歉,我是个新手,以后我会对剧透们放轻松的。。。但公平地说,在这个例子中,从拥有所有正确的逻辑到拥有正确的Ruby并不是一个巨大的飞跃。谢谢你纠正我的错误:-)我只是在开玩笑——回答别人的问题没关系!(除非看起来他们只是想在家庭作业上作弊)@Tom和Andy,我理解你的担心,但在这种情况下,这不是家庭作业。我以前学过Python,现在正试图自学Ruby和Rails。但说到编程,我还是个新手。:)在这方面,使用
sort
可以减少方法中的一次比较,同时降低整体清晰度@AndyRoyal提供了一个简单、可读、优雅的解决方案。我非常喜欢这个解决方案中的
return:fooif
。第二种方法很聪明——它让你思考。另一方面,你希望读者必须思考,还是立即理解?(一个形而上学的问题,或者至少是修辞性的问题,我将把它留给Quora这样的论坛:-)@Tom,也许我在uniq周围待得更久了,但第二种解决方案对我来说似乎非常简单,让我比其他解决方案思考得更少。