ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# remcopeereboom's solution

## to Triangle in the Ruby Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Determine if a triangle is equilateral, isosceles, or scalene.

An equilateral triangle has all three sides the same length.

An isosceles triangle has at least two sides the same length. (It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)

A scalene triangle has all sides of different lengths.

## Note

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. See Triangle Inequality.

## Dig Deeper

The case where the sum of the lengths of two sides equals that of the third is known as a degenerate triangle - it has zero area and looks like a single line. Feel free to add your own code/tests to check for degenerate triangles.

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby triangle_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride triangle_test.rb
``````

## Source

The Ruby Koans triangle project, parts 1 & 2 http://rubykoans.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### triangle_test.rb

``````require 'minitest/autorun'
require_relative 'triangle'

# Common test data version: 1.0.0 8acd78c
class TriangleTest < Minitest::Test
def test_triangle_is_equilateral_if_all_sides_are_equal
# skip
triangle = Triangle.new([2, 2, 2])
assert triangle.equilateral?, "Expected 'true', triangle [2, 2, 2] is equilateral."
end

def test_triangle_is_not_equilateral_if_any_side_is_unequal
skip
triangle = Triangle.new([2, 3, 2])
refute triangle.equilateral?, "Expected 'false', triangle [2, 3, 2] is not equilateral."
end

def test_triangle_is_not_equilateral_if_no_sides_are_equal
skip
triangle = Triangle.new([5, 4, 6])
refute triangle.equilateral?, "Expected 'false', triangle [5, 4, 6] is not equilateral."
end

def test_all_zero_sides_are_illegal_so_the_triangle_is_not_equilateral
skip
triangle = Triangle.new([0, 0, 0])
refute triangle.equilateral?, "Expected 'false', triangle [0, 0, 0] is not equilateral."
end

def test_equilateral_triangle_sides_may_be_floats
skip
triangle = Triangle.new([0.5, 0.5, 0.5])
assert triangle.equilateral?, "Expected 'true', triangle [0.5, 0.5, 0.5] is equilateral."
end

def test_triangle_is_isosceles_if_last_two_sides_are_equal
skip
triangle = Triangle.new([3, 4, 4])
assert triangle.isosceles?, "Expected 'true', triangle [3, 4, 4] is isosceles."
end

def test_triangle_is_isosceles_if_first_two_sides_are_equal
skip
triangle = Triangle.new([4, 4, 3])
assert triangle.isosceles?, "Expected 'true', triangle [4, 4, 3] is isosceles."
end

def test_triangle_is_isosceles_if_first_and_last_sides_are_equal
skip
triangle = Triangle.new([4, 3, 4])
assert triangle.isosceles?, "Expected 'true', triangle [4, 3, 4] is isosceles."
end

def test_equilateral_triangles_are_also_isosceles
skip
triangle = Triangle.new([4, 4, 4])
assert triangle.isosceles?, "Expected 'true', triangle [4, 4, 4] is isosceles."
end

def test_triangle_is_not_isosceles_if_no_sides_are_equal
skip
triangle = Triangle.new([2, 3, 4])
refute triangle.isosceles?, "Expected 'false', triangle [2, 3, 4] is not isosceles."
end

def test_sides_that_violate_triangle_inequality_are_not_isosceles_even_if_two_are_equal
skip
triangle = Triangle.new([1, 1, 3])
refute triangle.isosceles?, "Expected 'false', triangle [1, 1, 3] is not isosceles."
end

def test_isosceles_triangle_sides_may_be_floats
skip
triangle = Triangle.new([0.5, 0.4, 0.5])
assert triangle.isosceles?, "Expected 'true', triangle [0.5, 0.4, 0.5] is isosceles."
end

def test_triangle_is_scalene_if_no_sides_are_equal
skip
triangle = Triangle.new([5, 4, 6])
assert triangle.scalene?, "Expected 'true', triangle [5, 4, 6] is scalene."
end

def test_triangle_is_not_scalene_if_all_sides_are_equal
skip
triangle = Triangle.new([4, 4, 4])
refute triangle.scalene?, "Expected 'false', triangle [4, 4, 4] is not scalene."
end

def test_triangle_is_not_scalene_if_two_sides_are_equal
skip
triangle = Triangle.new([4, 4, 3])
refute triangle.scalene?, "Expected 'false', triangle [4, 4, 3] is not scalene."
end

def test_sides_that_violate_triangle_inequality_are_not_scalene_even_if_they_are_all_different
skip
triangle = Triangle.new([7, 3, 2])
refute triangle.scalene?, "Expected 'false', triangle [7, 3, 2] is not scalene."
end

def test_scalene_triangle_sides_may_be_floats
skip
triangle = Triangle.new([0.5, 0.4, 0.6])
assert triangle.scalene?, "Expected 'true', triangle [0.5, 0.4, 0.6] is scalene."
end

# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
#   VERSION = 1 # Where the version number matches the one in the test.
# end
#
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

def test_bookkeeping
skip
assert_equal 1, BookKeeping::VERSION
end
end``````
``````class TriangleError < RuntimeError; end

class Triangle
def initialize(a, b, c)
fail_unless_valid_sides(a, b, c)
@a, @b, @c = a, b, c
end

def kind
case
when equilateral? then :equilateral
when isosceles? then :isosceles
else :scalene
end
end

private

def fail_unless_valid_sides(a, b, c)
if a <= 0 || b <= 0 || c <= 0
fail TriangleError, "Sides must be > 0."
elsif a + b <= c || a + c <= b || b + c <= a
fail TriangleError, "A side must be less than the sum of the others."
end
end

def equilateral?
@a == @b && @b == @c
end

def isosceles?
@a == @b || @a == @c || @b == @c
end

def scalene?
!isosceles? && !equilateral
end
end``````