Ruby Koan 151 raising exceptions

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

    This one did take some brain time. But here's my solution

    def triangle(a, b, c)
      # WRITE THIS CODE
      raise TriangleError, "All sides must be positive number" if a <= 0 || b <= 0 || c <= 0
      raise TriangleError, "Impossible triangle" if ( a + b + c - ( 2 *  [a,b,c].max ) <= 0  )
    
      if(a == b && a == c)
          :equilateral
      elsif (a == b || b == c || a == c)
          :isosceles
      else
        :scalene
      end
    end
    

提交回复
热议问题