Ruby Koan 151 raising exceptions

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

    Here's my solution... honestly I can't think of a more concise and readable one!

    def triangle(a, b, c)
      raise TriangleError unless a > 0 && b > 0 && c > 0
      raise TriangleError if a == b && a + b <= c
      raise TriangleError if a == c && a + c <= b
      return :equilateral if a == b && b == c
      return :isosceles   if a == b || b == c || c == a
      :scalene
    end
    

提交回复
热议问题