views:

82

answers:

2

Hey guys, 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.
+1  A: 
  1. A triangle should not have any sides of length 0. If it does, it's either a line segment or a point, depending on how many sides are 0.
  2. Negative length doesn't make sense.
  3. Any two sides of a triangle should add up to more than the third side.
  4. See 3, and focus on the "more".

You shouldn't need to change the TriangleError code, AFAICS. Looks like your syntax is just a little wacky. Try changing

raise new.TriangleError

to

raise TriangleError, "why the exception happened"

Also, i'm not sure if Ruby cares, but whether it does or not, you should be testing the values (and throwing exceptions) before you do anything with them. Move the exception stuff to the beginning of the function.

cHao
Hi cHao, thanks for your comment! I undestand when to raise the exceptions, I just dont understand what goes in the TriangleError class
Elliot
Doesn't the code itself say "No need to change this code"? It should just work.
cHao
That was from part 1 of the class, in part 2 (this part) you do change the code
Elliot
You sure? People don't normally say "you don't need to change this" if they require you to change it.
cHao
I'm not 100% sure, I updated the question at the bottom. The koan won't let me proceed until I change something here.
Elliot
The problem is more likely that you're not throwing exceptions for the other 3 cases. Far as i see, the test is checking all 4 cases; even if you throw on the first one, the caller catches it and keeps going with the others. And all 4 cases should throw exceptions.
cHao
even when adding exceptions for the other 3 cases, the koan message doesn't change. Its also saying there is an error with the line: " assert_raise(TriangleError) do triangle(0, 0, 0) end"
Elliot
@Elliot: updated the answer.
cHao
Thank you so much cHao! you made my day!
Elliot
A: 

You definately do not update the TriangleError class - I am stuck on 152 myself. I think I need to use the pythag theorem here.

def triangle(a, b, c) # WRITE THIS CODE

if a == 0 || b == 0 || c == 0 raise TriangleError end

# The sum of two sides should be less than the other side if((a+b < c) || (a+c < b) || (b+c < a)) raise TriangleError end if a==b && b==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

end

Error class used in part 2. No need to change this code.

class TriangleError < StandardError end ~
~

Justin Miller