Open4
binding.irb/debugger/binding.pry

binding.irb/debugger/binding.pryを調べたきっかけ
- Railsの開発をしていると使うであろうデバッカたち
- ちょうど先日のイベントでデバッカの話をしていてつぶやいた
- そしたらこんなリプを頂いた
- これ関連の話で聞いた内容も含めてまとめてみる

binding.irb
使い方
- 処理を止めたい箇所に
binding.irb
を挟み込む
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 23], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:12:in `project_owner?'
From: /Users/nakayama_bird/workspace/everydayrails_rspec/everydayrails-rspec-jp-2024/app/controllers/tasks_controller.rb @ line 21 :
16: def edit
17: end
18:
19: # POST /tasks or /tasks.json
20: def create
=> 21: binding.irb
22: @task = @project.tasks.new(task_params)
23:
24: respond_to do |format|
25: if @task.save
26: format.html { redirect_to @project, notice: "Task was successfully created." }
irb:rdbg(#<TasksController:0x000000012...):055> task_params
#<ActionController::Parameters {"name"=>"新しいタスク", "completed"=>"0"} permitted: true>
irb:rdbg(#<TasksController:0x000000012...):056> debug
IRB is already running with a debug session.
もろもろメモ
- Rubyの組み込みライブラリで
require
なしで使うことができる - 手元にあった雑なFizzBuzzも何もせずとも処理を止めることができる
def fizz_buzz(n)
if n % 15 == 0
binding.irb
'Fizz Buzz'
elsif n % 3 == 0
#....
❯ ruby ruby-book/lib/fizz_buzz_chapter_02.rb
1
2
Fizz
4
Buzz
Fizz
Ignoring prism-0.24.0 because its extensions are not built. Try: gem pristine prism --version 0.24.0
From: ruby-book/lib/fizz_buzz_chapter_02.rb @ line 3 :
1: def fizz_buzz(n)
2: if n % 15 == 0
=> 3: binding.irb
4: 'Fizz Buzz'
5: elsif n % 3 == 0
6: 'Fizz'
7: elsif n % 5 == 0
8: 'Buzz'
irb(main):001> p n
15
=> 15
- break pointとして使う説明だと下記がわかりやすい
- Ruby3.2に組み込まれたIRB1.6あたりからインタラクティブなデバッカとして使えるようになったっぽい
- 上記の記事にもあるがそもそもbinding.irbでdebug向けのコマンドを実行すると
debug.gem
が動いている - そのため実際動いているデバッカの機能としてはdebug.gemと同じになる
irb(main):001> IRB.version
=> "irb 1.15.2 (2025-04-03)"
irb(main):001> show_cmds
Debugging
debug Start the debugger of debug.gem.
break Start the debugger of debug.gem and run its `break` command.
catch Start the debugger of debug.gem and run its `catch` command.
next Start the debugger of debug.gem and run its `next` command.
delete Start the debugger of debug.gem and run its `delete` command.
step Start the debugger of debug.gem and run its `step` command.
continue Start the debugger of debug.gem and run its `continue` command.
finish Start the debugger of debug.gem and run its `finish` command.
backtrace Start the debugger of debug.gem and run its `backtrace` command.
info Start the debugger of debug.gem and run its `info` command.
- だんだんパワーアップしており、Ruby3.3から
irb:rdbg
となり、IRBを終了せずにrdbgの全コマンドを利用できるようになっているらしい - だがしかしbinding.irbだと省略形(nextだとn、stepだとs)が使えない気がするのだが......

debugger
使い方
-
binding.break
/debugger
/binding.b
のいずれかを挟み込めば良い
require 'debug'
def fizz_buzz(n)
if n % 15 == 0
binding.b
'Fizz Buzz'
[1, 10] in ruby-book/lib/fizz_buzz_chapter_02.rb
1| require 'debug'
2| def fizz_buzz(n)
3| if n % 15 == 0
=> 4| binding.b
5| 'Fizz Buzz'
- 最近のRails開発でデバッカ使うなら正直これ一択なのかもと思う。周りもdebugger使っている人が圧倒的に多い印象。
- Railsガイドにも練習帳にもバッチリ使い方が書いてある。
もろもろメモ
- こちらもRubyライブラリ
- しかしながら標準ライブラリではないので私の雑なFizzBuzzに書いても認識してくれない
def fizz_buzz(n)
if n % 15 == 0
debugger
'Fizz Buzz'
elsif n % 3 == 0
#...
❯ ruby ruby-book/lib/fizz_buzz_chapter_02.rb
1
2
Fizz
4
Buzz
Fizz
ruby-book/lib/fizz_buzz_chapter_02.rb:3:in `fizz_buzz': undefined local variable or method `debugger' for main (NameError)
debugger
^^^^^^^^
from ruby-book/lib/fizz_buzz_chapter_02.rb:20:in `<main>'
-
require 'debug'
してあげれば使えます
require 'debug'
def fizz_buzz(n)
if n % 15 == 0
debugger
'Fizz Buzz'
elsif n % 3 == 0
❯ ruby ruby-book/lib/fizz_buzz_chapter_02.rb
1
2
Fizz
4
Buzz
Fizz
[1, 10] in ruby-book/lib/fizz_buzz_chapter_02.rb
1| require 'debug'
2| def fizz_buzz(n)
3| if n % 15 == 0
=> 4| debugger
5| 'Fizz Buzz'
6| elsif n % 3 == 0
7| 'Fizz'
8| elsif n % 5 == 0
9| 'Buzz'
10| else
=>#0 Object#fizz_buzz(n=15) at ruby-book/lib/fizz_buzz_chapter_02.rb:4
#1 <main> at ruby-book/lib/fizz_buzz_chapter_02.rb:21
(rdbg)
- Railsの場合だと7.0以降だとデフォルトでdevelopmentとtest環境にgem debugが入っているのでそのまま使える
↳ app/controllers/application_controller.rb:12:in `project_owner?'
[17, 26] in ~/workspace/everydayrails_rspec/everydayrails-rspec-jp-2024/app/controllers/tasks_controller.rb
17| end
18|
19| # POST /tasks or /tasks.json
20| def create
21| @task = @project.tasks.new(task_params)
=> 22| debugger
23|
24| respond_to do |format|
25| if @task.save
# Gemfile
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri windows]
- こちら使うと省略形(nextだとn、stepだとs)も確実に使用できる
(rdbg) s # step command
[8, 17] in ruby-book/lib/fizz_buzz_chapter_02.rb
8| elsif n % 5 == 0
9| 'Buzz'
10| else
11| n.to_s
12| end
=> 13| end
- Use rdbg with commands written in Rubyが気になってちょっと試した
- テストの立ち上がるめちゃくちゃ初期で止まっておもろい
rdbg -c -- bundle exec rspec spec/models/user_spec.rb
WARN: Unresolved or ambiguous specs during Gem::Specification.reset:
rdoc (>= 4.0.0)
Available/installed versions of this gem:
- 6.13.1
- 6.12.0
- 6.7.0
- 6.6.3.1
- 6.6.2
WARN: Clearing out unresolved specs. Try 'gem cleanup <gem>'
Please report a bug if this causes problems.
[4, 13] in ~/.rbenv/versions/3.3.0/bin/rspec
4| #
5| # The application 'rspec-core' is installed as part of a gem, and
6| # this file is here to facilitate running it.
7| #
8|
=> 9| require 'rubygems'
10|
11| Gem.use_gemdeps
12|
13| version = ">= 0.a"

binding.pryと歴史
- こちらはRubyで使うにせよ、Railsで使うにせよ初期の設定が必要(なので私はほぼ使わない)
- 使い方自体は今までの2つとそんなに変わらなかった記憶がある
- 個人的にわざわざgem追加しなくて使えるdebuggerやbinding.irbの方が便利じゃないか?と思っていた(スクールのカリキュラムでpry入ってたので)
- そんな話をしていたら比較的最近までirbがイマイチでデバッカとして別のツールを使ってというような話を聞いた
- そのあたり過去のことを調べれば出てくると思うよとのことだったのでちょっとみてみた
binding.irbの歴史
- Ruby2.4でbinding.irbが入った頃からの変遷があって面白い
- Ruby3.2になるまでやはりステップ実行ができなかったらしい
- binding.irbが深く潜れないの話はこのあたりからきてそうと思った(現状だとbinding.irbでやってもdebuggerでやっても同じそうなので)
- 読む感じ、しっかりデバッカとして使えるようになったのはRuby3.2以降のよう
debug gemの歴史
Rails
- Rails7.0からデフォルト導入
- それまではbyebugというのが使われていたらしい
- 一旦debug gemをデフォルトdemから外す動きがあったらしい(Rubyに一緒に同梱だしいいのでは?という感じ)
- 多分PRはこれ
-
しかしながら取り消されている
-
ざっと読む感じgemのアップデートに備えた感じっぽい
-
Rails上の確かにGemfileで管理しておけば最新版の取り込みが楽(Rubyに依存しない)そう
Ruby
- Ruby3.1から同梱
- 「debug gem: 新しいデバッガ」のあたりに記載あり
- それまでは、
lib/debug.rb
というのがバンドルされていたらしい(使う人もメンテする人もなかったらしい)