# How do I use irb and binding.irb to try and debug Ruby code?

Source: https://poignantguide.net/answers/read/how-do-i-use-irb-and-bindingirb-to-try-and-debug-ruby-code/
Site: Poignant Guide Ruby Q&A, category Errors and debugging help
Published: 2026-11-22. Updated: 2026-11-22. Replies: 0.

## Question by Fenwick Hale (staff), 2026-11-22

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.

## 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](https://docs.ruby-lang.org/en/3.3/IRB.html) 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](https://docs.ruby-lang.org/en/3.3/Binding.html) 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](https://poignantguide.net/ruby/expansion-pak-1.html). Its prompt looks like `irb(main):001:0>`. In Ruby 3.3 the prompt is `irb(main):001>`, with no trailing `:0`. Everything else you read there still applies.

## Try a line: plain irb

```bash
$ irb
irb(main):001> "hello".upcase
=> "HELLO"
irb(main):002> [1, 2, 3].map { |n| n * 2 }
=> [2, 4, 6]
irb(main):003> exit
```

Anything 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:

```ruby
# 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:

```bash
$ 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
1560
```

Ruby printed the lines around the pause, with an arrow on the current one. We asked for `tax_rate` and saw 25, not 0.25. The bug is not in the arithmetic. The caller passed a percentage where the method expects a fraction. Typing `exit` resumed 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.irb` hands that context to irb, so what you type is evaluated as if you had written it on that line. That is why `subtotal` was 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 |
| --- | --- |
| `help` | Lists the irb commands available in your version |
| `exit` | Leaves 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_total` |
| `ls` | Lists the methods and variables of the current object |

The two short aliases come from the documentation's own `conf.command_aliases` example. Run `help` to see what your version supports.

## Mistakes beginners make

1. **Leaving binding.irb in the file.** The program will stop for input every time it runs. Delete the line when you have your answer.
2. **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.
3. **Typing `exit` and expecting the program to stop.** In a binding.irb session, `exit` means continue. To end the whole program, press Ctrl-C.

## Check this in order

1. Read the error message and note the file and line number.
2. Put `binding.irb` on the line before that line and run the file again.
3. Inspect each variable on that line, one by one.
4. Compare what you see with what you assumed. The gap is the bug.
5. Try the fix at the prompt before editing the file.
6. Remove `binding.irb` and run again.

If the error was `undefined method for nil`, the thread [What does undefined method for nil:NilClass mean](/answers/read/what-does-undefined-method-for-nilnilclass-mean-and-how-do-i-fix-it/) shows what to look for at step 3. Output questions are covered in [the difference between puts, print and p](/answers/read/what-is-the-difference-between-puts-print-and-p-in-ruby/).

When you want to step line by line, the separate [ruby/debug](https://github.com/ruby/debug) gem adds `debugger` and `binding.break` with commands like `next`, `step` and `continue`. Start with binding.irb; reach for that when looking is not enough.

Checked on 12 November 2026.

---
Cite as: 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.
