Ruby Koan 151 raising exceptions

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

    I ended up with this code:

    def triangle(a, b, c)
        raise TriangleError, "impossible triangle" if [a,b,c].min <= 0
        x, y, z = [a,b,c].sort
        raise TriangleError, "no two sides can be < than the third" if x + y <= z
    
        if a == b && b == c # && a == c # XXX: last check implied by previous 2
            :equilateral
        elsif a == b || b == c || c == a
            :isosceles
        else
            :scalene
        end
    end 
    

    I don't like the second condition/raise, but I'm unsure how to improve it further.

提交回复
热议问题