开发者

How to write ruby code easier?(I mean in terminal write and then run it)

When I write a little ruby code, after a little bit, I always need to crea开发者_StackOverflow中文版te a new terminal tab to ruby it, to see if it's correct.

Are there any ways to do it in one window? Like a vim plugin or some other tool?


The following should work in vim, after you've saved the file:

:!ruby %

Or even

:!% 

This works under Linux when you have the correct "shebang" as the first line of the ruby file:

#!/usr/bin/env ruby

For extra fun, you can map this to a key in your ~/.vimrc:

map <F8> :!ruby %<CR>


Do you mean you need an interpreter to see what your code does? If so, check out irb.


The way you should check if your code works is using unit tests, not running it in the console or irb. Indeed, irb is a good solution for small fragment of code or to check for specific statements.

However, there are some solutions to your specific question. You can write the code in a file, save it and run it from the console.

ruby filename.rb

If you use TextMate, you can press ⌘ + R to execute the current code


Do as Simone Carletti said.

And for editing and saving your file suggest you Scite. http://www.scintilla.org/SciTEDownload.html here you can download it for many different operating systems. You get syntax highlighting in a very lightweight editor for almost everything (html, ruby, eruby, xml,...).

But you will need to have at least a Window Manager running.


in ~/.vimrc

autocmd FileType ruby imap <F8> <C-o>:w <CR> <C-o> :!ruby % <CR>

this way you can save and execute your file at once within insertion mode


In vim:

:!ruby %

will execute ruby on the current file. Remember to save it first!


If you are the Emacs type you should check out ruby-mode (which IIRC was written by Matz) and inf-ruby. See e.g. http://lathi.net/pages/emacs-ruby


You don't say what OS you're using, so I'm assuming either Linux or Mac-OS.

When you're at a command-line and using vim (not gvim) you can do a <CNTRL>+Z to temporarily halt the editor and return to the command-line. Issue any commands you need, then use "fg" to return to the editor.

There are times I'll use :!ruby % from inside vim (or gvim) but sometimes I need the real command line and if I'm ssh'd into a machine the <CNTRL>+Z trick is nice.


Agreed with @Simone Carletti. If you are learning the language and want to make sure that methods/classes are doing what you want then you can use irb.

There is a gem called interactive_editor which enable you to run vim inside irb (side-by-side actually). Watch this Vimcast for demo.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜