Ruby Koan 151 raising exceptions

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

    This is what I ended up with. It is sort of a combination of a few of the above examples with my own unique take on the triangle inequality exception (it considers the degenerate case as well). Seems to work.

    def triangle(a, b, c)
    
      raise TriangleError if [a,b,c].min <= 0 
      raise TriangleError if [a,b,c].sort.reverse.reduce(:-) >= 0
    
      return :equilateral if a == b && b == c
      return :isosceles   if a == b || a == c ||  b == c
      return :scalene 
    
    end
    

提交回复
热议问题