Ruby Koan 151 raising exceptions

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

    1. A triangle should not have any sides of length 0. If it does, it's either a line segment or a point, depending on how many sides are 0.
    2. Negative length doesn't make sense.
    3. Any two sides of a triangle should add up to more than the third side.
    4. See 3, and focus on the "more".

    You shouldn't need to change the TriangleError code, AFAICS. Looks like your syntax is just a little wacky. Try changing

    raise new.TriangleError
    

    to

    raise TriangleError, "why the exception happened"
    

    Also, you should be testing the values (and throwing exceptions) before you do anything with them. Move the exception stuff to the beginning of the function.

提交回复
热议问题