Skip to content

Instantly share code, notes, and snippets.

@elgalu
Created November 22, 2012 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elgalu/4132984 to your computer and use it in GitHub Desktop.
Save elgalu/4132984 to your computer and use it in GitHub Desktop.
Exceptional Ruby: Master the Art of Handling Failure in Ruby by Avdi Grimm
Great book! Here are my quiz notes:
- What is the diff bt raise and fail?
=> no diff
- Are raise//fail ruby keywords or methods?
=> Kernel#raise (are methods)
- Re-raise last exception
=>
# With no arguments will re-raise exception in $! If nil then RuntimeError
# note $! value is nil initialized as soon as the begin..rescue.end block terminates
raise
- What is the third argument of raise and give an example of what to put there.
=> the backtrace, use for example Kernel#caller
- What is catch/throw used for?
=> not related to exceptions, catch/throw allows you
to quickly exit blocks back to a point where a catch is defined for a specific symbol
- Diffs bt `redo` vs `retry`
=> Both ruby keywords are used to re-execute parts of a loop but:
redo only repeats the current iteration
retry repeats the whole loop from the start in 1.8 but in 1.9 gives SyntaxError: Invalid retry
Note retry should only be used inside immediate rescue blocks
- What can you use to ignore/continue/retry//etc on every ruby program error, console based?
=> the `hammertime` gem. just require 'hammertime'
- Test unit, describe the test helpers (assertions) for
equality, check nothing raised, check something raised.
=>
assert_equal val1, val2
assert_nothing_raised { block }
# ensure some exception is not raised
assert_nothing_raised ArgumentError { block }
# ensure something is raised
assert_raise { block }
# ensure some exception is raised
assert_raise NameError { puts asd }
- What's caller() method used for, where's defined and what param takes?
=>
Kernel#caller(omit_from_start=1)
# Returns the current execution stack.
# An array containing strings in the form “file:line” or “file:line: in `method'”
# Has an optional start parameter with the number of initial stack entries to omit from the result.
# when omit=0 it will also show the current file:line:method where caller() was called
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment