LoginSignup
3
7

More than 3 years have passed since last update.

Windows 10のVSCodeでrubyのdebug環境を構築する

Last updated at Posted at 2020-08-10

はじめに

一年以上ぶりに、rubyの環境を作ろうとしたら、なかなかうまくいかず、やっと環境ができたので、メモを残します。これで、rubyでstep実行や変数の確認ができるようになります。

Visual Studio Code のインストール

Ruby のインストール

VSCodeの拡張機能をインストール

  • VSCodeを起動して、拡張機能の「ruby」を検索して、インストールします。(下図はインストール後の画面) image.png

Gemをbundleでインストール

  • VSCodeのターミナルを開いてbundle initします。
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

新しいクロスプラットフォームの PowerShell をお試しください https://aka.ms/pscore6

PS C:\Users\momoandbanana\Documents\my_ruby_programs\debugenv> bundle init
Writing new Gemfile to C:/Users/momoandbanana/Documents/my_ruby_programs/debugenv/Gemfile
PS C:\Users\momoandbanana\Documents\my_ruby_programs\debugenv> 
  • するとGemfileが作成されるので、debaseruby-debug-ideのGemを追加します。
Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# gem "rails"
gem "debase" # 追加しました
gem "ruby-debug-ide" # 追加しました
  • VSCodeのターミナルでbundle installでGemをインストールします。
PS C:\Users\momoandbanana\Documents\my_ruby_programs\debugenv> bundle init
Writing new Gemfile to C:/Users/momoandbanana/Documents/my_ruby_programs/debugenv/Gemfile
PS C:\Users\momoandbanana\Documents\my_ruby_programs\debugenv> bundle install
Fetching gem metadata from https://rubygems.org/.....
Resolving dependencies...
Using rake 13.0.1
Using bundler 2.1.4
Using debase-ruby_core_source 0.10.9
Using debase 0.2.4.1
Using ruby-debug-ide 0.7.2
Bundle complete! 2 Gemfile dependencies, 5 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
PS C:\Users\momoandbanana\Documents\my_ruby_programs\debugenv> 

launch.jsonに設定を記載します。

  • rubyのプログラムを用意します。
main.rb
puts("hello ruby-debugger !")
  • launch.json自動作成させるために、VSCodeのメニューから実行 構成を開く を選びます。
    image.png

  • すると、どのような構成をしたいのか選択肢で質問されるのでrubyを選びます。
    image.png

  • さらにdebug local fileを選びます。
    image.png

  • するとlaunch.jsonファイルが出来上がるので、programの行を下記のように修正します。

launch.json
{
    // IntelliSense を使用して利用可能な属性を学べます。
    // 既存の属性の説明をホバーして表示します。
    // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Local File",
            "type": "Ruby",
            "request": "launch",
            // "program": "${workspaceRoot}/main.rb", コメントにしました。
            "program": "${file}", // 変更しました。
        }
    ]
}

デバッグを開始します

  • ソースファイルを開いてから、VSCodeの実行 デバッグの開始でデバッグします。下図はmain.rbの1行目にブレークポイントを設定して、ブレークされたところです。 image.png

以上です。
実際の作業は、githubで記録しました。出来上がった各設定ファイルはこのコミットにあるものです。

3
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7