Ruby Koan 151 raising exceptions

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

    I wanted a method that parsed all arguments effectively instead of relying on the order given in the test assertions.

    def triangle(a, b, c)
      # WRITE THIS CODE
      [a,b,c].permutation { |p| 
         if p[0] + p[1] <= p[2]
           raise TriangleError, "Two sides of a triangle must be greater than the remaining side."
         elsif p.count { |x| x <= 0} > 0
           raise TriangleError, "A triangle cannot have sides of zero or less length."
         end
      }
    
      if [a,b,c].uniq.count == 1
        return :equilateral
      elsif [a,b,c].uniq.count == 2
        return :isosceles
      elsif [a,b,c].uniq.count == 3
        return :scalene
      end
    end
    

    Hopefully this helps other realize there is more than one way to skin a cat.

提交回复
热议问题