Ruby Debugging and Bundler Cheat Sheet
Pausing Ruby code to inspect it, and reading the source of the gems a project uses.
Overview
| Tool | Use it to |
|---|---|
binding.irb |
open an interactive irb session at a line of running code |
debug gem (binding.break) |
step through code line by line |
bundle open |
open an installed gem's source in your editor |
Details
binding.irb
def multiplication(a, b)
binding.irb # execution stops here; inspect a and b, call methods
a * b
end
It ships with Ruby, so no extra gem is needed (pry and byebug were the older alternatives). Ctrl+D or exit resumes execution.
Reading a Gem's Source
bundle open rails # opens the gem in $BUNDLER_EDITOR or $EDITOR
EDITOR=code bundle open devise
bundle info devise # version and install path
Useful when the documentation is thin: read how the method you call is actually implemented.
References
- Ruby documentation: IRB -
binding.irband IRB commands. - debug gem on GitHub - the standard Ruby debugger.
- Bundler documentation: bundle open - opening a gem's source.
- Rails Database Console Cheat Sheet - the other Rails command-line tools I use.
- Programming Languages MOC - hub for language notes.
Review cards
Flashcard Why should `binding.irb` be removed before committing code?
A forgotten binding.irb can freeze a request or a background job in production, waiting for input that nobody will provide, potentially causing the application to hang indefinitely.
Quiz Which command would you use to open the source code of the 'devise' gem in your editor?
- bundle open devise
- bundle edit devise
- bundle info devise
- gem open devise
The bundle open command opens the gem's source in the editor specified by the environment variable $BUNDLER_EDITOR or $EDITOR.