Execute Ruby code in sublime text 2

2019-01-23 01:04发布

问题:

How can I run a Ruby file with ST2 and see the ouput? I thought I should use the build command. But if I have this:

def foobar
  "hello world"
end

puts foobar

and then press cmd + shift + b. All I see is

[Finished in 0.1s]

In textmate I could use the cmd + r (run command) and see all the ouput from the ruby script.

I'm I missing something?

回答1:

I didn't save the file. That's why it didn't work. To run a file without saving it first (like TextMate) you should try to use the Anypreter plugin.



回答2:

I'm using windows here, but make sure you have selected the correct build system: "Tools > Build System > Ruby", then just press CTRL+B



回答3:

For future reference, hope it helps somebody:

I came here with the same problem. (In Sublime Text 3) I followed the answers:

  • selected ruby extention (bottom right in window)
  • selected in menubar: Tools > Build System > Ruby
  • save the file with a name
  • pressed command + b

and still saw:

[Finished in 0.1s]

In the end the (stuppid) problem was: the console window was only one line big. So I only saw the last line, which is the note:

[Finished in 0.1s]

solution: make the console window bigger et voila. I couldn't find any setting for that. So in the end I did that with the mouse Screenshot Sublime Text 3 - cropped

note the cursor (and ruby extention)



回答4:

In summary a Sublime text has an "Automatic" Build System by default (after install) and you should changed it to a ruby or whatever language you use. Save the file and press ctrl + b (on windows) - should run the output into the ST console.



回答5:

enter control + ` (key close to number 1 in keyboard so it will open new window bottom of sublime text, where you can enter ruby code and see result), but make sure you have install ruby correctly and set your path from terminal