Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active December 19, 2015 18:59
Show Gist options
  • Save havenwood/6003024 to your computer and use it in GitHub Desktop.
Save havenwood/6003024 to your computer and use it in GitHub Desktop.
A Ruby Triangle Class.
class Triangle
InvalidTriangleError = Class.new ArgumentError
attr_accessor :a, :b, :c
def initialize a, b, c
@a, @b, @c = [a, b, c].sort
raise InvalidTriangleError unless valid_triangle?
@type = triangle_type
end
private
def valid_triangle?
!!(@a > 0 && @a + @b > @c)
end
def triangle_type
[:equilateral, :isosceles, :scalene][[@a, @b, @c].uniq.size - 1]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment