且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Visual Studio Code上调试Ruby代码?

更新时间:2021-12-25 09:02:46

按照正确的步骤进行操作实际上很容易. 首先,您必须下载 Ruby扩展,它可以在vs代码市场上获得,也可以直接通过VS中的扩展"选项卡获得代码本身:只需搜索Ruby,安装它,然后重新加载VS Code [更新:vscode可以完美加载新扩展,而无需重新加载]. 其次,您必须遵循此扩展的调试指南,该指南可在我提供的github链接上或在vs代码市场中找到.这是您最会感兴趣的部分:

This is actually fairly easy once you follow the right steps. first you have to download Ruby Extension which is available on vs code marketplace or simply via Extension tab in VS code itself: just search for Ruby, install it, and reload VS Code [update: vscode can perfectly load new extensions without needing a reload]. secondly, you have to follow the debugging guide for this extension which is available on the github link I provided or in vs code marketplace. here's the section you would be most intrested in:

安装Ruby依赖项

Install Ruby Dependencies

在此扩展中,我们实现ruby debug ide协议以允许VS 与ruby debug进行通信的代码,它需要ruby-debug-ide 安装在您的计算机上.这也是RubyMine/NetBeans所做的工作 默认.

In this extension, we implement ruby debug ide protocol to allow VS Code to communicate with ruby debug, it requires ruby-debug-ide to be installed on your machine. This is also how RubyMine/NetBeans does by default.

  • 如果您使用的是JRuby或Ruby v1.8.x(jruby,ruby_18,mingw_18),请运行

  • If you are using JRuby or Ruby v1.8.x (jruby, ruby_18, mingw_18), run

    gem install ruby-debug-ide.

  • 如果您使用的是Ruby v1.9.x(ruby_19,mingw_19),请运行

  • If you are using Ruby v1.9.x (ruby_19, mingw_19), run

           gem install ruby-debug-ide.
    

    确保将ruby-debug-base19x与ruby-debug-ide一起安装.

    Make sure ruby-debug-base19x is installed together with ruby-debug-ide.

  • 如果您使用的是Ruby v2.x

  • If you are using Ruby v2.x

        gem install ruby-debug-ide
        gem install debase (or gem install byebug)
    

  • 将VS Code配置添加到您的项目中

    Add VS Code config to your project

    转到VS Code的调试器视图,然后点击齿轮图标.选择红宝石 或Ruby Debugger的提示窗口中,然后您将获得示例 在.vscode/launch.json中启动配置.样本启动配置 包括RSpec的调试器(完整且有效的spec文件),以及 黄瓜跑.这些示例期望捆绑安装--binstubs 已被调用.调试Ruby的详细说明 脚本/栏目/等

    Go to the debugger view of VS Code and hit the gear icon. Choose Ruby or Ruby Debugger from the prompt window, then you'll get the sample launch config in .vscode/launch.json. The sample launch configurations include debuggers for RSpec (complete, and active spec file) and Cucumber runs. These examples expect that bundle install --binstubs has been called. Detailed instruction for debugging Ruby Scripts/Rails/etc

    阅读以下有关如何在本地调试ruby/rails/etc的说明 或远程

    Read following instructions about how to debug ruby/rails/etc locally or remotely

    01 调试器安装

    02 从VS Code启动

    03附加到调试器

    04运行gem脚本

    05示例配置

    如果遵循这些步骤,则将在步骤1中安装所有依赖项. 第2步可帮助您配置项目工作区,以开始调试用ruby编写的代码. 通过完成步骤2,您应该能够开始调试. 这是一个简单的配置,我在最近的ruby项目中使用它来调试当前打开的文件.我在上面链接的第二步中对此进行了详细说明.

    if you follow these steps, you will have every dependency installed in Step 1. Step 2 helps you config your project workspace to start debuging codes written in ruby. by finishing step 2, you should be able to start debugging. here is a simple config that I use in my recent ruby project to simply debug the current open file. this is fully explained in the second step I linked above.

    {
         "name": "Debug Local File",
         "type": "Ruby",
         "request": "launch",
         "cwd": "${workspaceRoot}",
         "program": "${file}"
    }
    

    程序":"$ {file}"是用于调试当前打开文件的行.

    "program": "${file}" is the line that enables debugging the current open file.