Skip to content

Instantly share code, notes, and snippets.

@serbrech
Created April 5, 2011 18:55
Show Gist options
  • Save serbrech/904250 to your computer and use it in GitHub Desktop.
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?
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