Ruby Koan 151 raising exceptions

Viewed 23533

I'm going through the ruby koans, I'm on 151 and I just hit a brick wall.

Here is the koan:

# You need to write the triangle method in the file 'triangle.rb'
require 'triangle.rb'

class AboutTriangleProject2 < EdgeCase::Koan
  # The first assignment did not talk about how to handle errors.
  # Let's handle that part now.
  def test_illegal_triangles_throw_exceptions
    assert_raise(TriangleError) do triangle(0, 0, 0) end
    assert_raise(TriangleError) do triangle(3, 4, -5) end
    assert_raise(TriangleError) do triangle(1, 1, 3) end
    assert_raise(TriangleError) do triangle(2, 4, 2) end
 end
end

Then in triangle.rb we have:

def triangle(a, b, c)
  # WRITE THIS CODE
  if a==b && a==c
    return :equilateral
  end
  if (a==b && a!=c) || (a==c && a!=b) || (b==c && b!=a)
    return :isosceles
  end
  if a!=b && a!=c && b!=c
    return :scalene
  end
  if a==0 && b==0 && c==0
    raise new.TriangleError
  end



end

# Error class used in part 2.  No need to change this code.
class TriangleError < StandardError

end

I am beyond confused - any help at all would be much appreciated!

EDIT: To complete this koan, I need to put something in the TriangleError class - but I have no idea what

UPDATE: Here is what the koan karma thing is saying:

<TriangleError> exception expected but none was thrown.
38 Answers

You don't need to modify the Exception. Something like this should work;

def triangle(*args)
  args.sort!
  raise TriangleError if args[0] + args[1] <= args[2] || args[0] <= 0
  [nil, :equilateral, :isosceles, :scalene][args.uniq.length]
end

You could also try to instance the exception with:

raise TriangleError.new("All sides must be greater than 0") if a * b * c <= 0

My solution, I think it's one of the more readable ones:

def triangle(a, b, c)
  a, b, c = [a, b, c].sort
  if a <= 0 or c >= a + b
    raise TriangleError
  end
  case [a, b, c].uniq.length
  when 1
    :equilateral
  when 2
    :isosceles
  when 3
    :scalene
  end
end

This is my solution:-

def triangle(a, b, c)
  if a <= 0 || b <= 0 || c <= 0 || a + b <= c  || a + c <= b ||  b + c <= a
    raise TriangleError
  elsif (a == b) &&( a==c) && (b==c)
    return :equilateral
  elsif (a==b) || (b==c) || (a==c)
    return :isosceles
  else
    return :scalene
  end
end

Hope it helps.

SIMPLEST SOLUTION!

  def triangle(a, b, c)
  #This would check for if the numbers are negative or 0.
  if a <= 0 || b <= 0 || c <= 0 
    raise TriangleError
  end
  #This would check if the two sides of a triangle are greater than the remaining side.
  if a + b <= c  || a + c <= b ||  b + c <= a
    raise TriangleError
  end 

  if a == b && b = c
    :equilateral
  elsif a == b || a == c || b == c 
    :isosceles
  else 
    :scalene
  end
end

Thanks to all the beautiful people for their answer I have checked them all and I would say no need to change the TriangleError code. we need to check for invalid triangles and raise the error if the triangle is not.

The triangle.rb file need these update to work.

def triangle(a, b, c)
    # If length is Less Then or equal to 0 the triangle not possible
    # it raise error. If the length of any two sum is less or equal to other-
    # Again the triangle not possible.
    raise TriangleError if (a <= 0 || b <= 0 || c <= 0) || (a+b <= c || b+c <= a || c+a <= b)  

    # If the size of each equal its Equilateral Triangle.       
    return :equilateral if (a == b && a == c)

    # If any two lenght is equal than it Isosceles Triangle.
    return :isosceles if (a == b || b == c || a == c)

    # If each lenght is different than its Scalene Triangle.
    return :scalene if (a != b) && (b != c) && (a != c)
end  

class TriangleError < StandardError
end
Related