ruby/debug と連携してデバッグを行える VSCode extension が出てるので試す。
marketplace.visualstudio.com github.com
前提
$code -v 1.57.0 b4c1bd0a9b03c749ea011b06c6d2676c8091a70c x64 $ruby -v ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-darwin19]
準備
debug はプレリリース版をインストールしてねと README にあるので従う。
gem install debug --pre
ためしに以下のようなコードを書いてブレークポイントを貼ってみる
class A attr_accessor :x end def main a = A.new a.x = 2 puts a.x end main
Debug: Start Debugging and Stop on Entry
をコマンドパレットから選択する。初回は .vscode/launch.json
が生成される。ちなみに生成しなくても Debug command line からファイルを指定してあげるとそのファイルをデバッガ経由で実行してくれる。単一のファイルのみ確認したいとかだったらこれで十分。
{ // IntelliSense を使用して利用可能な属性を学べます。 // 既存の属性の説明をホバーして表示します。 // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "rdbg", "name": "Debug current file with rdbg", "request": "launch", "script": "${file}", "args": [], "askParameters": true }, { "type": "rdbg", "name": "Attach with rdbg", "request": "attach" } ] }
実行してブレークポイントで停止するとその時点のコールスタックと変数が見れて便利。
VSCode デバッガのステップイン等にもちゃんと対応してるし、ウォッチ式で停止している箇所のbindingでコードの実行結果を色々試せて捗りそう。いつも binding,pry を張って試しているので新鮮な気持ちになれた。GUIなデバッガ使うのVC++触ってた時以来かもしれない。