Skip to content

Instantly share code, notes, and snippets.

@jbevarts
Last active December 18, 2015 11:19
Show Gist options
  • Select an option

  • Save jbevarts/5774520 to your computer and use it in GitHub Desktop.

Select an option

Save jbevarts/5774520 to your computer and use it in GitHub Desktop.
Begin/rescue/end
# nice
def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
nil.some_method_nil_doesnt_know_about
rescue ex = Exception.new
assert_equal NilClass, ex.class
assert_match(/NoMethodError/, ex.message)
end
# not nice
def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
begin
nil.some_method_nil_doesnt_know_about
rescue Exception => ex
assert_equal NoMethodError, ex.class
assert_match(/NilClass/, ex.message)
end
end
# in the first example, do i need to use end to end the rescue clause, or can i just leave it in there solo. I find that either way works, and i prefer to not use begin/end.
# also, is there any advantage to using "=>" over "Class.new". I prefer Class.new, and i believe that => is an older syntax for 1.8 perhaps.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment