views:

134

answers:

2

I have recently tried sharping my rails skills with this tool:

http://github.com/edgecase/ruby_koans

but I am having trouble passing some tests. Also I am not sure if I'm doing some things correctly since the objective is to just pass the test, there are a lot of ways in passing it and I may be doing something that isn't up to standards.

Is there a way to confirm if I'm doing things right?

a specific example:

in about_nil,

 def test_nil_is_an_object
   assert_equal __, nil.is_a?(Object), "Unlike NULL in other languages"
 end

so is it telling me to check if that second clause is equal to an object(so i can say nil is an object) or just put assert_equal true, nil.is_a?(Object) because the statement is true?

and the next test:

def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
  # What happens when you call a method that doesn't exist.  The
  # following begin/rescue/end code block captures the exception and
  # make some assertions about it.
  begin
    nil.some_method_nil_doesnt_know_about
  rescue Exception => ex
    # What exception has been caught?
    assert_equal __, ex.class

    # What message was attached to the exception?
    # (HINT: replace __ with part of the error message.)
    assert_match(/__/, ex.message)
  end
end

Im guessing I should put a "No method error" string in the assert_match, but what about the assert_equal?

+1  A: 

assert_equal true, nil.is_a?(Object) is indeed the correct solution. The question is "Are nils in Ruby objects or not?", and in Ruby's case, they are. Thus, in order to pass the assertion, you should assert the truth of that test.

In the second example, when you call an undefined method on nil, you get NoMethodError: undefined method 'foo' for nil:NilClass. Thus, the exception class is NoMethodError, and the message is undefined method 'foo' for nil:NilClass. Test the failing behavior in a console, and see what you get from it, and then apply that knowledge to the test.

Chris Heald
thanks for the heads up! so does that mean that i will have to trust my instincts to know if what I did was right? Is there no way to check if what I did was correct and true to the ruby convention?
corroded
A: 

Well, in holding with the typical TDD motto of Red-Green-Refactor, you should run the test (probably with rake in a separate console) and see the failure happen. From there, they have provided you a few pieces of information about what was expected.

As for style, the koans aren't really teaching that. You should just find and read some code written in ruby to get a feel for the typical conventions and idioms of the ruby community.

Andy_Vulhop