Ruby Koan 151 Raising Exceptions

Ruby Koan 151 raising exceptions

  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, 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.

Ruby Koan 151 raising exceptions

  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, 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.

RubyKoans: broken koan?

Oh, I tested this koan. The error is on line 21 if you noticed that, not the "test_calling_global_methods_without_parentheses" method. It's the "test_sometimes_missing_parentheses_are_ambiguous" method goes wrong as it should be. You are expected to correct that method.

def test_calling_global_methods_without_parentheses
result = my_global_method 2, 3
assert_equal 5, result # You're fine with this koan.
end

# (NOTE: We are Using eval below because the example code is
# considered to be syntactically invalid).
def test_sometimes_missing_parentheses_are_ambiguous
eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK
# **LOOK HERE~~~ HERE IS THE ERROR YOU SEE** Just correct it.

And if there is any koan you don't know how to deal with, just comment it.

Understanding koan 121

So, again, my question is: what does private :my_private_method do?

private is actually just a regular method, defined on Module, Module#private.

From the documentation for that method (emphasis mine):

privateself


private(symbol, ...)self


private(string, ...)self


With no arguments, sets the default visibility for subsequently
defined methods to private. With arguments, sets the named methods to
have private visibility.
String arguments are converted to symbols.

module Mod
def a() end
def b() end
private
def c() end
private :a
end
Mod.private_instance_methods #=> [:a, :c]

Ruby koan 124 - Are these symbols spontaneously changing type?

No, symbols cannot change type on their own, but when you're using one or two in your example, you are using variables with similar names, provided by Ruby based on your method definition, not the symbols themselves.

Consider a hash: when you have a hash a = { one: 1, two: '1234' } and you write a[:one] you don't get a symbol, but an appropriate value. So :one still is a symbol, but with [a[:one], a[:two]] you will get [1, '1234'] array, not [:one, :two].



Related Topics



Leave a reply



Submit