5e1e28bfab6042cbbd20a8486ea65814c795bcd7
[ruby_koans.git] / koans / about_nil.rb
1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
2
3 class AboutNil < EdgeCase::Koan
4   def test_nil_is_an_object
5     assert_equal __, 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     # make 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 __, 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