How do I use irb and binding.irb to try and debug Ruby code?
Type irb in a terminal to get a scratch pad where every line of Ruby runs the moment you finish it. To debug a real program, write binding.irb on its own line, run the file, and Ruby stops there and opens the same prompt inside that spot, with your local variables ready to inspect.
Cite
Poignant Guide Ruby Q&A, "How do I use irb and binding.irb to try and debug Ruby code?", https://poignantguide.net/answers/read/how-do-i-use-irb-and-bindingirb-to-try-and-debug-ruby-code/, accessed 2026-11-24.- Fenwick HaleStaffStaff: gems, Rails, tooling and debugging ·
Two tools, one prompt
irb (Interactive Ruby) is a read-eval-print loop, or REPL: it reads a line, evaluates it, prints the result and waits for the next. The Ruby 3.3 IRB documentation describes it as a shell-like interface to the interpreter. It ships with Ruby, so there is nothing to install.
binding.irb is a method call you put inside your own program. It pauses the program and opens an irb session in the middle of the code that is running. The Binding documentation says it "allows for interactive debugging": you can call any method or read any variable in scope, and change state if you need to.
The Poignant Guide introduces irb in Expansion Pak 1. Its prompt looks like
irb(main):001:0>. In Ruby 3.3 the prompt isirb(main):001>, with no trailing:0. Everything else you read there still applies.Try a line: plain irb
$ irb irb(main):001> "hello".upcase => "HELLO" irb(main):002> [1, 2, 3].map { |n| n * 2 } => [2, 4, 6] irb(main):003> exitAnything you would hesitate to put in a file first is a good fit here: what does this method return, what does this hash look like, does this regular expression match.
exit(or Ctrl-D) leaves the session.Stop a real program: binding.irb
Here is a small program with a bug. The total should be 75.0 but prints something huge:
# order_total.rb def order_total(prices, tax_rate) subtotal = prices.sum binding.irb subtotal * (1 + tax_rate) end puts order_total([10, 20, 30], 25)Run it:
$ ruby order_total.rb From: order_total.rb @ line 4 : 3: subtotal = prices.sum => 4: binding.irb 5: subtotal * (1 + tax_rate) irb(main):001> subtotal => 60 irb(main):002> tax_rate => 25 irb(main):003> exit 1560Ruby printed the lines around the pause, with an arrow on the current one. We asked for
tax_rateand saw 25, not 0.25. The bug is not in the arithmetic. The caller passed a percentage where the method expects a fraction. Typingexitresumed the program, which then printed 1560 (60 times 26). Your exact output format may differ a little by version.Why this works
A binding is an object that remembers the execution context at one place in your code: the local variables, the value of
self, and the methods you can call.binding.irbhands that context to irb, so what you type is evaluated as if you had written it on that line. That is whysubtotalwas visible at the prompt: it already existed at that point. It also means a variable assigned on line 5 or later does not exist yet.Commands worth knowing
Command What it does helpLists the irb commands available in your version exitLeaves the session; with binding.irb, the program resumes whereami(or@)Shows the code around the pause again show_source(or$)Shows the source of a method or class, for example show_source order_totallsLists the methods and variables of the current object The two short aliases come from the documentation's own
conf.command_aliasesexample. Runhelpto see what your version supports.Mistakes beginners make
- Leaving binding.irb in the file. The program will stop for input every time it runs. Delete the line when you have your answer.
- Placing it after the line that fails. If the error is raised on line 3, a pause on line 5 never runs. Put it before the suspect line.
- Typing
exitand expecting the program to stop. In a binding.irb session,exitmeans continue. To end the whole program, press Ctrl-C.
Check this in order
- Read the error message and note the file and line number.
- Put
binding.irbon the line before that line and run the file again. - Inspect each variable on that line, one by one.
- Compare what you see with what you assumed. The gap is the bug.
- Try the fix at the prompt before editing the file.
- Remove
binding.irband run again.
If the error was
undefined method for nil, the thread What does undefined method for nil:NilClass mean shows what to look for at step 3. Output questions are covered in the difference between puts, print and p.When you want to step line by line, the separate ruby/debug gem adds
debuggerandbinding.breakwith commands likenext,stepandcontinue. Start with binding.irb; reach for that when looking is not enough.Checked on 12 November 2026.
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.