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

When I write a little ruby code, after a little bit, I always need to create 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.

Related

Globally Map "Escape Key" to "jj" in vim (mac)

I have been using vim for awhile but I have not been able to figure out how to map the escape globally - every time I open a file, I have to map the escape key to jj like this:
:imap jj <Esc>
Is there a way to change the runtime defaults so that Esc is already mapped to jj?
I found the default.vim file, but you cannot edit it on a mac (with standard mac software). Should I download something to edit this file? Is there another easier way?
Any help is much appreciated!
The place for your local changes is called .vimrc and you typically create one in your home directory with the commands you want to execute on startup.
Run vimtutor on the (system) command line and spend a few minutes learning how to use this editor. Then dig into the facilities provided by :help in Vim. E.g. run :help vimrc in Vim.

how to compile a ruby code on my vim-editor for very simple and easy way... or some trick

I'm trying to learn VIM editor , and I'm going to test some small code
for ruby
here is question..
When I compile and launch my small ruby code,
Command-mode,
:!ruby %
Or Open another terminal,
$ruby filename.rb
I did ,
but.. its really stressful and I cannot focus....
Is there any magical things?? ,
config .vimrc file then make a hot-key
or... make some script by rubyself...??
You can use :!! to repeat the last :!{cmd}.
You can type :!ruby % once, and then run :!!, which can of course be mapped to a key like:
nnoremap <F8> :!!<CR>
Or to write and run it (saves typing :w):
nnoremap <F8> :w<CR>:!!<CR>
This is a flexible solution, since you can replace :!ruby % with anything else (e.g. :!coffee -c %, :!python %, etc.).
If your vim distribution has ruby support, can be confirmed using vim --version |grep ruby, Then
nnoremap <F9> :rubyfile %<CR>
This is will be more faster as this does not invoke an external command using !ruby and hence no shell launch. More details at :help ruby.
Another way to complete the work irrespective of ruby support.
Your vim distribution will come with $VIMRUNTIME/compiler/ruby.vim for compiler setting. If so, you can set it :compiler ruby for ruby files in your vimrc.
This will allow you to just do make to accomplish what you are doing. But output looks somewhat clumsy.
Hence, Having a some keybinding will help
nnoremap <f9> :make<CR> :copen<CR>
This will open a quickfix for error. You can just press F9 or any other key that you have mapped.
You might also like to review quickfix commands at :help quickfix
You can try doing this to your .vimrc. It's not exactly sophisticated, but may be good enough for your needs:
" Run current file as Ruby program
nnoremap <C-r> :!ruby %<CR>

Are there vi / vim shortcuts available in pry or irb?

I have muscle memory of the vi/vim commands. So in bash, I use the vi mode,
for example, I can easily go back to my command history and re-edit previous commands.
Are vi shortcuts available with pry or irb? If so, how do I set it up?
Thank you.
Not sure about re-editing previous commands, but you could use interactive editor gem to start vim-like editing from inside of your irb. Using that, you can start irb, edit your script in vim and let ruby shell execute it immediately. Here is a great tutorial on this: Running Vim within IRB.

Execute Code in VIM similar to Cmd+R in Textmate or Cmd+B Sublime Text2

Hey is there a way or plugin to execute Code e.g. Ruby in my case, directly from my vim editor. I know this from Textmate, where you can execute Code with Cmd+R or Cmd+B in Sublime Text2. In Sublime Text it is called Build System.
Thanks for advise!
If you just want to execute the current buffer in Ruby, you could do this in normal mode:
:!ruby %
You could also map that to a shortcut with your leader key:
:map <leader>r :!ruby %<cr>
Which would let you do leader+r to run the file.
My vim has a :rubydo command, select the section of code you want to execute (or nothing to execute the whole buffer), and do
:rubydo
"'<,'>" will be added automatically after ":" if something was selected.
that should to the trick
Well, one simple thing you can use is to execute a command in your shell with :!.
# Typing typing typing...
# Oh! Gotta commit.
:!hg ci -m "Add awesome module xyz"
Or you can use :shell to drop into the shell, if you're going to be doing more complex things.
Use the :!<anything want bash or the calling shell of vim to execute>. So if you have a script named foo.rb, to execute it from within the vim editor call :!ruby foo.rb.
ruby-runner is a vim plugin that allows you to run ruby script from within vim, the good part is you can see your code output along with the your code at the same time.
https://github.com/henrik/vim-ruby-runner
IMHO the better option is to use rcodetools.

How do I reformat ruby code from the command line?

Given a source file with bad indentation, incorrect white space management, and so on. How do I reformat the code such a file and apply the Ruby's style guide (if present)?
This should be a task of your editor. In vim (configured for Ruby), just press gg=G xD
A Ruby script that does it is available at: http://www.arachnoid.com/ruby/rbeautify.rb.html
Haven't tried it myself:
http://www.arachnoid.com/ruby/rubyBeautifier.html

Resources