Errors and debugging
When a program goes wrong, Ruby raises. This part covers exceptions and the classes of your own, cleanup that must run whatever happens, warnings and logging, the debug gem, and the tracing and profiling tools that show where the time and the bug are.
7.1
Exceptions
Handle errors gracefully with raise, rescue, and ensure in Ruby.
Open
7.2
Debugging
Inspect values with p and pp, warn on stderr, pause with the debug gem, and halve the code.
Open
7.3
Exception classes and custom errors
Where an error class belongs in the StandardError tree, a project error that carries the id that failed, and the cause that keeps the original exception.
Open
7.4
ensure, at_exit and cleanup
ensure runs whether the method returns or raises, a file block closes the handle for you, at_exit runs at the end of the process, and a temporary directory removes itself.
Open
7.5
warn, warnings and log levels
warn writes to stderr while puts writes to stdout, ruby -w reports unused variables and other smells, and Logger gives a warning a level and a timestamp.
Open
7.6
Debugging with the debug gem
Install the debug gem, put binding.break where the program goes wrong, and use next, step, finish and continue to walk the code and read the locals.
Open
7.7
TracePoint, benchmark and profilers
Time a block with Benchmark.measure, watch every call with TracePoint, read the heap size from GC.stat, and take the first profile with stackprof.
Open