Created
March 29, 2013 22:15
-
-
Save michaelrkn/5274054 to your computer and use it in GitHub Desktop.
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 | |
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