Last active
December 19, 2015 18:59
-
-
Save havenwood/6003024 to your computer and use it in GitHub Desktop.
A Ruby Triangle Class.
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
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