mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-15 07:23:19 -04:00
Added src directory
This commit is contained in:
38
src/about_nil.rb
Normal file
38
src/about_nil.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
require 'edgecase'
|
||||
|
||||
class AboutNil < EdgeCase::Koan
|
||||
def test_nil_is_an_object
|
||||
assert_equal __(true), nil.is_a?(Object), "Unlike NULL in other languages"
|
||||
end
|
||||
|
||||
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 __(NoMethodError), ex.class
|
||||
|
||||
# What message was attached to the exception?
|
||||
# (HINT: replace __ with part of the error message.)
|
||||
assert_match(/#{__("undefined method")}/, ex.message)
|
||||
end
|
||||
end
|
||||
|
||||
def test_nil_has_a_few_methods_defined_on_it
|
||||
assert_equal __(true), nil.nil?
|
||||
assert_equal __(""), nil.to_s
|
||||
assert_equal __("nil"), nil.inspect
|
||||
|
||||
# THINK ABOUT IT:
|
||||
#
|
||||
# Is it better to use
|
||||
# obj.nil?
|
||||
# or
|
||||
# obj == nil
|
||||
# Why?
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user