0
0
Rubyprogramming~10 mins

Debugging with pry and byebug in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Debugging with pry and byebug
Start Program
Hit binding.pry or byebug
Pause Execution
Interact: Inspect Variables, Step, Continue
Resume or Exit Debugger
Program Continues or Ends
The program runs until it hits a debugger call, then pauses allowing you to inspect and control execution before continuing.
Execution Sample
Ruby
require 'pry'
def add(a, b)
  binding.pry
  a + b
end

add(2, 3)
This code pauses execution inside the add method so you can inspect variables before returning the sum.
Execution Table
StepActionCode LineVariables StateDebugger PromptOutput
1Start programdef add(a, b)a: undefined, b: undefinedNo
2Call add(2, 3)add(2, 3)a: 2, b: 3No
3Hit binding.prybinding.prya: 2, b: 3Yes - pry prompt
4Inspect variablesbinding.prya: 2, b: 3Yes - pry prompta=2, b=3
5Step over to next linea + ba: 2, b: 3Yes - pry prompt
6Continue executiona + ba: 2, b: 3No5
7Program ends
💡 Program ends after returning the sum and no more code to run.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6Final
aundefined2222
bundefined3333
Key Moments - 3 Insights
Why does the program pause at binding.pry or byebug?
The debugger call tells Ruby to stop running and open an interactive prompt so you can inspect and control the program (see execution_table step 3).
Can I see variable values before the debugger line?
No, variables get their values when the method is called. Before that, they are undefined (see variable_tracker start values).
How do I continue running the program after stopping at the debugger?
You type 'continue' or 'c' at the debugger prompt to resume normal execution (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of variable 'a' at step 4?
A2
Bundefined
C3
Dnil
💡 Hint
Check the 'Variables State' column at step 4 in the execution_table.
At which step does the debugger prompt first appear?
AStep 2
BStep 5
CStep 3
DStep 6
💡 Hint
Look for 'Yes - pry prompt' in the 'Debugger Prompt' column.
If you remove 'binding.pry' from the code, what changes in the execution table?
AVariables become undefined
BDebugger prompt never appears
CProgram never ends
DOutput changes to nil
💡 Hint
Without the debugger call, the program runs straight through without pausing (see exit_note).
Concept Snapshot
Use binding.pry or byebug to pause Ruby code execution.
At the pause, interactively inspect variables and control flow.
Commands: 'next' to step, 'continue' to resume.
Remove debugger calls to run normally.
Great for finding bugs by watching code run step-by-step.
Full Transcript
This visual trace shows how Ruby's debugging tools pry and byebug pause program execution at a specific line. When the program hits binding.pry, it stops and opens a prompt where you can check variable values and step through code. Variables get their values when the method is called, so before that they are undefined. You continue running the program by typing 'continue' at the prompt. Removing the debugger call lets the program run without stopping. This helps find bugs by letting you watch what happens inside your code.