Ruby Koans Triangle

Yesterday I came to find this exercise pretty interesting, it called back the distant geometry lessons I’ve had in the past.

This was compromised of two separate test files and one which we will code the project named triangle.rb, was accomplished by first passing the test for building a triangle and last to raise the Exceptions or errors.
My answer was the following one:

def triangle(a, b, c)
  if a <= 0 || b <= 0 || c <= 0
    raise TriangleError, "Size must be over 0."
end

  if ((a+b) <= c || ((a+c) <= b) || ((b+c) <= a)
    raise TriangleError, "Illegal triangle!"
  end

# end of error raise

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

class TriangleError < StandardError
end

This might not be the most neat and correct Ruby syntax but I’ll get there.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s