Skip to content

Instantly share code, notes, and snippets.

@innerfence
Created August 21, 2010 00:33
Show Gist options
  • Save innerfence/541523 to your computer and use it in GitHub Desktop.
Save innerfence/541523 to your computer and use it in GitHub Desktop.
def triangle(a, b, c)
sides = [ a, b, c ]
unless sides.all? { |s| s > 0 }
raise TriangleError, 'Sides must be positive'
end
s1, s2, s3 = sides.sort
unless s1 + s2 > s3
raise TriangleError, 'Sides violate the triangle inequality'
end
case sides.uniq.count
when 1 then :equilateral
when 2 then :isosceles
else :scalene
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment