Created
April 5, 2011 18:55
-
-
Save serbrech/904250 to your computer and use it in GitHub Desktop.
My ruby koans triangle implementation. Is there a "rubyer" way to write this?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def guard(a, b, c) | |
if a < 0 || b < 0 || c < 0 | |
raise TriangleError, "a triangle should not have a side with a negative value." | |
end | |
if a == 0 && b == 0 && c == 0 | |
raise TriangleError, "A triangle should not have all its side equal to 0." | |
end | |
if (a + b) <= c || (b + c) <= a || (a + c) <= b | |
raise TriangleError, "Any two sides of a triangle should add up to more than the third side." | |
end | |
end | |
def triangle(a, b, c) | |
guard a, b, c | |
return :equilateral if a == b && b == c | |
return :isosceles if a == b || b == c || a == c | |
:scalene | |
end | |
# Error class used in part 2. No need to change this code. | |
class TriangleError < StandardError | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment