Ruby Koan 151 raising exceptions
- 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.
- Negative length doesn't make sense.
- Any two sides of a triangle should add up to more than the third side.
- 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
- 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.
- Negative length doesn't make sense.
- Any two sides of a triangle should add up to more than the third side.
- 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):
private
→self
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
Ruby on Rails: What Reporting And/Or Charting Tools Are Available
Error When Starting Rails Server: Warning: Insecure World Writable Dir /Usr in Path, Mode 040777
Rails Convert String to Number
Should I Use Class Method or Instance Method, and Why
Rubymine: Rails Server Launcher Wasn't Found in the Project
How to Override Static Class Method Using Module in Ruby
Generate Ruby Classes from Xsd
Creating an Empty File in Ruby: "Touch" Equivalent
How to Rescue Model Transaction and Show the User an Error
How to Fire and Forget a Subprocess
No Implicit Conversion of String into Integer (Typeerror)
Possible to Alias a Belongs_To Association in Rails
Errno::Enoent: No Such File or Directory Ruby
How to Monitor Uptime of 20 Websites (Ping or Http) in Node.Js/Ror
Ruby/Rails Using || to Determine Value, Using an Empty String Instead of a Nil Value