Open4

binding.irb/debugger/binding.pry

なかじなかじ

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.

もろもろメモ

https://docs.ruby-lang.org/ja/latest/method/Binding/i/irb.html

  • 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として使う説明だと下記がわかりやすい

https://github.com/ruby/irb/blob/master/README.md#the-bindingirb-breakpoint

  • Ruby3.2に組み込まれたIRB1.6あたりからインタラクティブなデバッカとして使えるようになったっぽい

https://techracho.bpsinc.jp/hachi8833/2023_01_16/126191#debug

  • 上記の記事にもあるがそもそも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)が使えない気がするのだが......

https://techracho.bpsinc.jp/hachi8833/2023_12_27/138271#debug

なかじなかじ

debugger

使い方

  • binding.break/debugger/binding.bのいずれかを挟み込めば良い

https://github.com/ruby/debug?tab=readme-ov-file#invoke-with-the-debugger

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ガイドにも練習帳にもバッチリ使い方が書いてある。

https://railsguides.jp/debugging_rails_applications.html#debug-gemでデバッグする

https://zenn.dev/igaiga/books/rails-practice-note/viewer/ruby_rails_debug_gem

もろもろメモ

  • こちらもRubyライブラリ

https://github.com/ruby/debug
https://docs.ruby-lang.org/ja/latest/library/debug.html

  • しかしながら標準ライブラリではないので私の雑な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

https://railsguides.jp/debugging_rails_applications.html#debug-gemでデバッグする

# 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が気になってちょっと試した

https://github.com/ruby/debug?tab=readme-ov-file#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入ってたので)

https://github.com/pry/pry

  • そんな話をしていたら比較的最近までirbがイマイチでデバッカとして別のツールを使ってというような話を聞いた
  • そのあたり過去のことを調べれば出てくると思うよとのことだったのでちょっとみてみた

binding.irbの歴史

  • Ruby2.4でbinding.irbが入った頃からの変遷があって面白い
  • Ruby3.2になるまでやはりステップ実行ができなかったらしい
  • binding.irbが深く潜れないの話はこのあたりからきてそうと思った(現状だとbinding.irbでやってもdebuggerでやっても同じそうなので)

https://qiita.com/k0kubun/items/4600e52a1a245495dfaa
https://k0kubun.hatenablog.com/entry/2021/04/02/211455

  • 読む感じ、しっかりデバッカとして使えるようになったのはRuby3.2以降のよう

debug gemの歴史

Rails

  • Rails7.0からデフォルト導入

https://techracho.bpsinc.jp/hachi8833/2021_11_22/113636
https://github.com/rails/rails/pull/43187

  • それまではbyebugというのが使われていたらしい

https://github.com/deivid-rodriguez/byebug

  • 一旦debug gemをデフォルトdemから外す動きがあったらしい(Rubyに一緒に同梱だしいいのでは?という感じ)

https://y-yagi.hatenablog.com/entry/2023/02/28/052508

  • 多分PRはこれ

https://github.com/rails/rails/pull/47515

  • しかしながら取り消されている

  • ざっと読む感じgemのアップデートに備えた感じっぽい

  • Rails上の確かにGemfileで管理しておけば最新版の取り込みが楽(Rubyに依存しない)そう

https://github.com/rails/rails/pull/47823

Ruby

  • Ruby3.1から同梱
  • 「debug gem: 新しいデバッガ」のあたりに記載あり

https://www.ruby-lang.org/ja/news/2021/12/25/ruby-3-1-0-released/

  • それまでは、lib/debug.rbというのがバンドルされていたらしい(使う人もメンテする人もなかったらしい)

https://techlife.cookpad.com/entry/2021/12/27/202133