Created
September 4, 2013 00:55
-
-
Save steven-ferguson/6431554 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
require './lib/triangles' | |
def main | |
@sides = [] | |
puts "Please enter a side length" | |
@sides << gets.chomp | |
puts "Please enter another side length" | |
@sides << gets.chomp | |
puts "Please enter a third side length" | |
@sides << gets.chomp | |
@sides.map! do |side| | |
side.to_i | |
end | |
@triangle = Triangle.new(@sides[0], @sides[1], @sides[2]) | |
puts "Your triangle is #{@triangle.type}." | |
puts "\n\n" | |
main | |
end | |
main | |
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(side1, side2, side3) | |
@side1 = side1 | |
@side2 = side2 | |
@side3 = side3 | |
end | |
def type | |
if @side1 == @side2 && @side1 == @side3 | |
"equilateral" | |
elsif (@side1 == @side2 || @side1 == @side3) && valid_triangle? | |
"isosceles" | |
elsif (@side1 != @side2 || @side1 != @side3) && valid_triangle? | |
"scalene" | |
else | |
"not a triangle" | |
end | |
end | |
def valid_triangle? | |
sides = [@side1, @side2, @side3] | |
small_sides = @side1 + @side2 + @side3 - sides.max | |
if sides.max >= small_sides | |
false | |
else | |
true | |
end | |
end | |
end |
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
require 'rspec' | |
require 'triangles' | |
describe Triangle do | |
it 'is initialized with three side lengths' do | |
test_triangle = Triangle.new(3, 3, 3) | |
test_triangle.should be_an_instance_of Triangle | |
end | |
describe "type" do | |
it 'returns equilateral when all three sides are equal' do | |
test_triangle = Triangle.new(3,3,3) | |
test_triangle.type.should eq "equilateral" | |
end | |
it 'returns isosceles when two sides are equal' do | |
test_triangle = Triangle.new(4, 4, 3) | |
test_triangle.type.should eq "isosceles" | |
end | |
it 'returns scalene if no sides are equal' do | |
test_triangle = Triangle.new(3,4,5) | |
test_triangle.type.should eq "scalene" | |
end | |
it 'returns "not a triangle" if one side is greater than or equal to the sum of the other two sides' do | |
test_triangle = Triangle.new(6, 2, 1) | |
test_triangle.type.should eq "not a triangle" | |
end | |
end | |
describe 'valid_triangle?' do | |
it 'checks if the triangle is a valid triangle' do | |
test_triangle = Triangle.new(6,2,1) | |
test_triangle.valid_triangle?.should eq false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment