編集中の Ruby スクリプトをその場で実行する

Vim で編集中の Ruby スクリプトを、即時実行できるように設定した。

上記サイトを参考に、.vimrc に追記。

function! Ruby_eval_vsplit() range
let src = tempname()
let dst = "RubyOutput"
" put current buffer's content in a temp file
silent execute ": " . a:firstline . "," . a:lastline . "w " . src
" open the preview window
silent execute ":pedit! " . dst
" change to preview window
wincmd P
" set options
setlocal buftype=nofile
setlocal noswapfile
setlocal syntax=none
setlocal bufhidden=delete
" replace current buffer with ruby's output
silent execute ":%! ruby " . src . " 2>&1 "
" change back to the source buffer
wincmd p
endfunction
autocmd FileType ruby vmap <silent> <C-J> :call Ruby_eval_vsplit()<cr>
autocmd FileType ruby nmap <silent> <C-J> mzggVG<C-J>`z

Linux 環境では "Ruby Output" がエラーになるので "RubyOutput" に変更。また、FileType が ruby のときのみ動作するようにした。