Skip to content

Instantly share code, notes, and snippets.

@michaelrkn
Created March 29, 2013 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelrkn/5274054 to your computer and use it in GitHub Desktop.
Save michaelrkn/5274054 to your computer and use it in GitHub Desktop.
class Triangle
def initialize(sides)
@sides = sides
end
def type
@sides.sort!
if @sides[0] + @sides[1] <= @sides[2]
:invalid
else
case @sides.uniq.length
when 1
:equilateral
when 2
:isosceles
when 3
:scalene
end
end
end
end
require 'rspec'
describe Triangle do
it 'is initialized with three sides as arguments' do
Triangle.new([1,2,3]).should be_an_instance_of Triangle
end
it 'is equilateral if all three sides are equal' do
triangle = Triangle.new([1,1,1])
triangle.type.should eq :equilateral
end
it 'is isosceles if only two sides are equal' do
triangle = Triangle.new([2,2,3])
triangle.type.should eq :isosceles
end
it 'is scalene if no sides are equal' do
triangle = Triangle.new([2,4,3])
triangle.type.should eq :scalene
end
it 'is invalid if one side is at least as long as the other two combined' do
triangle = Triangle.new([1,2,3])
triangle.type.should eq :invalid
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment