Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Koan: test_nil_is_an_object

Tags:

ruby

I have recently tried sharpening 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 just to 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?

like image 771
corroded Avatar asked Aug 25 '10 05:08

corroded


3 Answers

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.

like image 193
Chris Heald Avatar answered Sep 20 '22 23:09

Chris Heald


Are you running

ruby path_to_enlightenment.rb

at the command prompt after you correct each test? It will give you lots of help.

Also "remember that silence is sometimes the best answer" -- if you are stumped don't put in anything and the tool will help you.

like image 5
Les Avatar answered Sep 24 '22 23:09

Les


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.

like image 2
Andy_Vulhop Avatar answered Sep 23 '22 23:09

Andy_Vulhop