Ruby Koan 151 raising exceptions

后端 未结 30 1589
孤独总比滥情好
孤独总比滥情好 2021-01-31 14:34

I\'m going through the ruby koans, I\'m on 151 and I just hit a brick wall.

Here is the koan:

# You need to write the triangle method in the file \'trian         


        
30条回答
  •  生来不讨喜
    2021-01-31 14:59

    I don't think I see this one here, yet.

    I believe all the illegal triangle conditions imply that the longest side can't be more than half the total. i.e:

    def triangle(a, b, c)
    
      fail TriangleError, "Illegal triangle: [#{a}, #{b}, #{c}]" if
        [a, b, c].max >= (a + b + c) / 2.0
    
      return :equilateral if a == b and b == c
      return :isosceles if a == b or b == c or a == c
      return :scalene
    
    end
    

提交回复
热议问题