Ruby 3.2のIRBに導入された新機能(翻訳)
IRB 1.6がリリースされ、Ruby 3.2の組み込みIRBにはこのバージョンが使われる予定です(訳注: IRBの現時点の最新バージョンは1.6.2です)。
今回のリリースも含め、最近のIRBには@k0kubunと私が行った多くの拡張が含まれているので、本記事でそれらをまとめてご紹介したいと思います。
新しいコマンド
最近のリリースでは以下のような多くの新しいコマンドをIRBに追加しました。
show_cmdsshow_doceditdebugおよび以下のデバッグ関連コマンドbreakcatchnextdeletestepcontinuefinishbacktraceinfo
新しいコマンドの数が多くて億劫に感じるかもしれないので、ひとつずつ紹介していきます。
🔗 show_cmds
show_cmdsコマンドは、IRBで利用できる全コマンドと説明を出力します。これはPryやByebugやdebugのhelpコマンドと似たような機能です。
(コマンド名をhelpにしなかった理由は、IRBのhelpコマンドが既にAPIドキュメント検索用に使われているからです。詳しくは次のセクションで説明します。)
IRB v1.6のshow_cmds出力結果は以下のとおりです。
IRB
cwws Show the current workspace.
chws Change the current workspace to an object.
workspaces Show workspaces.
pushws Push an object to the workspace stack.
popws Pop a workspace from the workspace stack.
irb_load Load a Ruby file.
irb_require Require a Ruby file.
source Loads a given file in the current session.
irb Start a child IRB.
jobs List of current sessions.
fg Switches to the session of the given number.
kill Kills the session with the given number.
irb_info Show information about IRB.
show_cmds List all available commands and their description.
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.
Misc
edit Open a file with the editor command defined with `ENV["EDITOR"]`.
measure `measure` enables the mode to measure processing time. `measure :off` disables it.
Context
show_doc Enter the mode to look up RI documents.
ls Show methods, constants, and variables. `-g [query]` or `-G [query]` allows you to filter out the output.
show_source Show the source code of a given method or constant.
whereami Show the source code around binding.irb again.
🔗 show_doc
show_docはhelpコマンドのエイリアスです。
前述したように、IRBのhelpコマンドは他の競合ツールとかなり異なっていて、ユーザーが混乱したり頭にきたりすることがあります。
そこで、IRB v1.6以降はshow_docコマンドの利用をユーザーに促したいと思います。さらに、次のメジャーリリースではhelpコマンドでコマンド情報を表示するように変更する予定です。
helpコマンドを使ったことのない方は、show_doc String#gsubを試してみてください-)
訳注
Rubyをビルドするときに--disable-install-rdocや--disable-install-docを指定した場合は、上を実行してもドキュメントは表示されません。
🔗 edit
editは、ENV["EDITOR"]で定義されたエディタでファイルを開きます。
edit- 現在のコンテキストのファイルを開く
edit path/to/foo.rb- foo.rbを開く
edit Foo- ソース内で
Fooの位置を探索して開く edit Foo#bar- ソース内で
Foo#barの位置を探索して開く
デモ画面

🔗 debugコマンドとその他デバッグ関連コマンド
show_cmdsの出力を見てみると、Debuggingセクションが大きな位置を占めているのがわかります。
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.
これらはすべて、IRBとdebug gemをつなぎ合わせるために私たちが追加したものです。
debugコマンドは以下の2つを行います。
- 現在のIRBセッションでdebug gemを
requireしていなかった場合は、代わりにrequireしてくれます。 - 続いて現在のコンテキストで
debugのデバッグセッションを開始します。これはbinding.bのブレークポイントに入るのと同じです。
From: test.rb @ line 4 :
1: a = 1
2: b = 2
3:
=> 4: binding.irb
5:
6: c = 3
irb(main):001:0> debug
[1, 6] in test.rb
1| a = 1
2| b = 2
3|
=> 4| binding.irb
5|
6| c = 3
=>#0 <main> at test.rb:4
(rdbg) # you're now using the debugger
(注: 上はbinding.irbでIRBセッションを開始した場合にのみ有効な動作です。irb実行ファイルをデバッグする意味はないからです。)
その他のデバッグコマンド(<cmd>で表記)は、debug + <cmd>の略記です。
たとえば、debugを実行してからstepしたいとします。この場合はIRBで単にstepを実行すればデバッグセッションが開始されるので、続いてstepを実行します。
From: test.rb @ line 1 :
=> 1: binding.irb
2:
3: a = 1
4: b = 2
5:
irb(main):001:0> step
(rdbg:irb) step
[1, 4] in test.rb
1| binding.irb
2|
=> 3| a = 1
4| b = 2
=>#0 <main> at test.rb:3
(rdbg)
長期的な目標は、IRBをdebug gemのインターフェイスにすることです(ちょうどpry-byebugが提供しているような感じで)。つまり、IRBセッションから離れなくても特定のdebugコマンドを実行できるようにするということです。
しかしそれまでは、これらのコマンドのショートカットを用いて場面を切り替えやすくしたいと考えています。
🔗 REPLによるデバッグ(binding.irb)vs デバッガによるデバッグ(binding.b)
REPL1では、ブレークポイント(binding.irb)周囲のコンテキストにしかアクセスできません。
たとえば、barメソッド内にbinding.irbを置いたとすると、そのメソッド内部のローカル変数しか見えません。
def foo(n)
bar(n + 1)
end
def bar(x)
binding.irb # xしか見えない
baz(x + 10)
end
しかしデバッガ内部なら、binding.bブレークポイントまたは新しいdebugコマンドを用いることで以下を行えます。
stepやnextなどのコマンドでプログラムの実行位置を移動できるstepを実行するとbazメソッドのコンテキストに入る
upやdownコマンドを用いて現在のコールスタック上のフレームを行き来できるfooに戻ってnの値を調べることができる
breakやcatchコマンドでブレークポイントを動的に設定できる
デバッガーの詳しい使い方については、ruby/debug - The best investment for your productivityの私のトークをご視聴ください。
🔗 コマンドの改善
show_sourceでPry風の構文(show_source Class#method)をサポート- 新しいエイリアス記号が追加された
$:show_sourceのエイリアス@:whereamiのエイリアス
- .irbrcファイル内の
IRB.conf[:COMMAND_ALIASES]でエイリアスを追加・変更可能になった- 例:
IRB.conf[:COMMAND_ALIASES].merge!({ :'&' => :show_cmds })で&をshow_cmdsのエイリアスにできる
- 例:
lsに追加された-g/-G引数で出力を正規表現でgrepできるようになった- 例:
ls -g irb:irbにマッチするメソッドだけを出力する
- 例:
🔗 その他の改良
- クラッシュが発生する多くの問題が修正された
- READMEが更新され、利用可能なすべてのコマンドをここで参照できるようになった
🔗 新しいコンフィグ
ENV["EDITOR"]:editコマンドでファイルを開くときに使われるENV["IRB_USE_AUTOCOMPLETE"]:"false"を指定するとIRBのオートコンプリート機能が無効になる- Rails 7.0.5および7.1以降では、production環境でのオートコンプリートを無効にするために使われるようになる(#46656)
🔗 まとめ
これらの変更によって、IRBがさらに便利で使いやすいものになることを願っています。可能なら、最新のバージョン1.6をインストールして新機能を試してみてください。
何か問題がありましたら、issueをオープンしてください。Ruby 3.2がリリースされるまでにできるだけ多くの問題を検出しておきたいと思います。
今後もこうしたニュースを本ブログに掲載しますが、Rails at Scale blogを購読いただけるとさらに多くのコンテンツをお楽しみいただけます ;-)
概要
元サイトの許諾を得て翻訳・公開いたします。