# exercise is from http://rubykoans.com/ - a great way to learn ruby! #! my not so succinct but works solution def triangle(a, b, c) validate_triangle(a, b, c) if a == b && b == c :equilateral elsif a != b && b != c && c != a :scalene else :isosceles end end def validate_triangle(a, b, c) raise TriangleError, "No such thing as negative or 0-length sides in a triangle" if a <= 0 || b <= 0 || c <= 0 raise TriangleError, "Incomplete triangle because the sides cannot touch and close up" if a + b <= c || a + c <= b || b + c <= a end # === # Josh's beautiful solution taken from https://stackoverflow.com/a/14445542/6463816 # === def triangle(a, b, c) [a, b, c].permutation do |sides| raise TriangleError unless sides[0] + sides[1] > sides[2] end case [a,b,c].uniq.size when 3; :scalene when 2; :isosceles when 1; :equilateral end end class TriangleError < StandardError end