seeker
seeker

Reputation: 558

How do I execute a Ruby script using VSCode?

I'm using MacOS and I have installed VSCode, Ruby, rubocop, Ruby Solargraph, and their gem dependencies.

Having done this, how do I now execute a Ruby script without using the Terminal panel within VSCode?

Upvotes: 25

Views: 48234

Answers (5)

Artur INTECH
Artur INTECH

Reputation: 7276

May 2023 update:

There are multiple ways:

1. Using tasks. The simplest and recommended way.

Add the following task to your project's .vscode/tasks.json file and then run it:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run ruby file",
            "type": "shell",
            "command": "ruby ${file}",
            "presentation": {
                "reveal": "always",
                "panel": "shared",
                "showReuseMessage": false,
                "clear": true
            },
        }
    ]
}

You might want to remap the F5 key in keybindings.json to execute that task instead of the default behavior which fires a debugger. This is what I personally prefer to do.

{
    "key": "f5",
    "command": "-workbench.action.debug.start",
},
{
    "key": "f5",
    "command": "workbench.action.tasks.runTask",
    "args": "Run ruby file", /* Label of the task defined above */
    "when": "editorTextFocus && editorLangId == 'ruby'"
}

2. Using debugger (clumsier)

  1. Install Ruby LSP extension from Shopify (Shopify.ruby-lsp).
  2. Go to the "Debug" panel and click on the create a launch.json file link.
  3. Run Debug: Start Without Debugging action.

The downside of this method is that it prints the additional information from a debugger.

Ruby REPL: You can run any Ruby expression here. Note that output to the STDOUT/ERR printed on the TERMINAL. [experimental] ,COMMAND runs COMMAND debug command (ex: ,info). ,help to list all debug commands. DEBUGGER: Disconnected.

Upvotes: 4

Yaroslav  Yenkala
Yaroslav Yenkala

Reputation: 49

You can try "Ruby Runner" extension for VS Code. It's works well in 2021.

https://marketplace.visualstudio.com/items?itemName=MateuszDrewniak.ruby-runner

Upvotes: 4

Asyraf
Asyraf

Reputation: 657

You have 2 choices for this case;

    • Install Code runner extension

    • Then , CTRL + ALT + N


    • Install Code runner extension

    • Cmd + Shift + p

    • choose Run By language

    • type ruby

wallla 🔥

Upvotes: 18

Voontent
Voontent

Reputation: 755

CTRL+ALT+N after installing Coder Runner Extension

Upvotes: 12

Antarr Byrd
Antarr Byrd

Reputation: 26071

You can use Coder Runner Extension.

Once adding the extension you press Command + Shift + P and select run by language then choose ruby

Upvotes: 32

Related Questions