[Ruby] Build a ruby debug environment with Windows 10 VS Code

1 minute read

Introduction

When I tried to create a ruby environment for the first time in more than a year, it didn’t work very well, and I finally made an environment, so I’ll leave a note. Now you can use ruby to execute steps and check variables.

Install Visual Studio Code

  • Download from the Microsoft page and install.

Install Ruby

Install the #VSCode extension

  • Start VSCode, search for “ruby” of the extension, and install it. (The screen below shows the screen after installation) image.png

Install Gem with bundle

  • Open VS Code terminal and do bundle init.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform 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>
  • Then, Gemfile is created, so add the debase and ruby-debug-ide gems.

Gemfile


# frozen_string_literal: true

source "https://rubygems.org"

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

# gem "rails"
gem "debase" # added
gem "ruby-debug-ide" # added
  • Install gem with bundle install in VS Code terminal.
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>

The settings are described in # lunch.json.

  • Prepare the ruby program.

main.rb


puts("hello ruby-debugger !")
  • To have __lunch.json automatically created, select Run Open Configuration from the VS Code menu. image.png

  • Then, you are asked what kind of configuration you want to make, so select ruby. image.png

  • And select debug local file. image.png

  • Then, the launch.json file is created, so modify the program line as follows.

launch.json


{
    // You can use IntelliSense to learn the available attributes.
    // Hover and display the description of existing attributes.
    // For more information, please check: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Local File",
            "type": "Ruby",
            "request": "launch",
            // "program": "${workspaceRoot}/main.rb", commented out.
            "program": "${file}", // changed.
        }
    ]
}

Start debugging

  • Open the __ source file, and from __, debug with VS Code Run Start debugging. The figure below shows a place where a break has been set by setting a breakpoint on the first line of main.rb. image.png

that’s all. The actual work was recorded on github.Eachcompletedconfigurationfileisinthiscommit.