Skip to content

Instantly share code, notes, and snippets.

@ivanbrennan
Last active December 25, 2015 02:19
Show Gist options
  • Select an option

  • Save ivanbrennan/6901611 to your computer and use it in GitHub Desktop.

Select an option

Save ivanbrennan/6901611 to your computer and use it in GitHub Desktop.
triangle.rb
class TriangleError < Exception
end
class Triangle
attr_reader :sides
def initialize(side1, side2, side3)
@sides = [side1, side2, side3].sort
self.validate
end
def validate
min, mid, max = sides
raise TriangleError if min <= 0
raise TriangleError if max >= min + mid
end
def kind
min, mid, max = sides
if min == max
:equilateral
elsif [min, max].include? mid
:isosceles
else
:scalene
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment