Ruby Koan 151 raising exceptions

后端 未结 30 1591
孤独总比滥情好
孤独总比滥情好 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 15:19

    Leon wins on fancy elegance, Benji for his knowledge the Array API. Here's my brute elegant answer:

    def triangle(a, b, c)
       [a, b, c].each { | side | raise TriangleError, "Sides must be positive" unless side > 0 }
       raise TriangleError, "Two sides can never be less than or equal to third side" if ((a + b) <= c) | ((a + c) <= b) | ((b + c) <= a)
    
       return :equilateral if (a == b) && (b == c)
       return :isosceles if (a == b) || (b == c) || (a == c)
       return :scalene
    end
    

提交回复
热议问题