Advertisement
urbanslug

about_nil.rb

Oct 23rd, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. require File.expand_path(File.dirname(__FILE__) + '/neo')
  2.  
  3. class AboutNil < Neo::Koan
  4. def test_nil_is_an_object
  5. assert_equal true, nil.is_a?(Object), "Unlike NULL in other languages"
  6. end
  7.  
  8. def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
  9. # What happens when you call a method that doesn't exist. The
  10. # following begin/rescue/end code block captures the exception and
  11. # makes some assertions about it.
  12. begin
  13. nil.some_method_nil_doesnt_know_about
  14. rescue Exception => ex
  15. # What exception has been caught?
  16. assert_equal false, ex.class
  17.  
  18. # What message was attached to the exception?
  19. # (HINT: replace __ with part of the error message.)
  20. assert_match(/__/, ex.message)
  21. end
  22. end
  23.  
  24. def test_nil_has_a_few_methods_defined_on_it
  25. assert_equal __, nil.nil?
  26. assert_equal __, nil.to_s
  27. assert_equal __, nil.inspect
  28.  
  29. # THINK ABOUT IT:
  30. #
  31. # Is it better to use
  32. # obj.nil?
  33. # or
  34. # obj == nil
  35. # Why?
  36. end
  37.  
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement