How to run a Ruby script in Sublime Text 2 from pr

2019-02-20 16:26发布

问题:

I have a Ruby project and I'd like to run the scripts directly from Sublime Text in place of having to run them in the terminal. I'm running Ubuntu 12.04 LTS if that matters at all.

From the terminal I need to be located in the project root to make the script work:

$ ruby path/to/file/file.rb

If I try to run it from where the file is located it fails because they are requiring files from various places in the project.

If I run the script in Sublime Text (Ctrl + Shift + B) it fails the same way as I'm running it from the file location.

How do I do this? I suspect this can be solved with a custom build but I've been unsuccessful until now. Any help appreciated.

回答1:

A build file like this might meet your requirements:

{
  "working_dir": "/full/path/to/project/root/",
  "cmd": ["/full/path/to/ruby/", "$file"],
  "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
  "selector": "source.ruby"
}


回答2:

Depending on your scripts you might want to look into using Package Control and the SublimeREPL Package.

SublimeREPL allows you to have a Ruby Interpreter in a separate tab or window in SublimeText. Running CMD + Shift + B has never been very reliable in SublimeText for Ruby code anyways.

Also, similar answer to similar question: Execute Ruby code in sublime text 2