ruby irb on windows using gitbash shell - can't use arrow keys to modify command input? - ruby

when I do rails console my git bash shell permits me to use up arrow to recall commands, and use left/right arrows to modify the text I'm entering
when I run irb the shell ignores backspace and arrow keys
I'm not sure why the arrow keys would work fine in the shell for rails console but not when running irb?

As I just wrote in the related Backspace and arrow keys aren't working in IRB(Git Bash console) on windows machine:
running irb with --noreadline solved this problem for me:
irb --noreadline

What operating system are you running? You may need install the GNU Readline Library and reinstall ruby.

Doing the following command fixed the problem for me on Windows
gem install wirble win32console

As documented here https://groups.google.com/forum/#!topic/rubyinstaller/HgswOz1T-eE, use the below command/alias:
alias irb="ruby -S irb"

Related

Rubyinstaller for Windows - ruby does nothing

I've tried using Ruby 2.0 x64 and Ruby 1.9.3 for Windows using RubyInstaller. Entering ruby -v works as expected, and running gem gives me the expected usage docs. Running and using the Interactive Ruby application works as expected. I am running Windows 8.1 Update.
However, for both installations, running ruby from cmd gives me a blank prompt where I can type, but nothing is executed when I press enter. If I attempt to install a gem, there is a similar issue where the program is running, but there is absolutely no output, and nothing happens.
I can't seem to be able to find a similar issue elsewhere. Does anyone know what might be wrong, and how I could fix it?
What did you expect to happen? ruby.exe is the ruby interpreter, meant for running ruby scripts. Normally, to use it you would create a file containing valid ruby commands with your favorite text editor (but not a word processor). If you save the file as foobar.rb, typing ruby foobar.rb (or if you told the installer to associate .rb files with ruby, typing just foobar.rb) will execute the commands in the file as a script/program. If you don't supply a script file name, ruby goes into input mode and expects you to type in a program on the spot. It won't give any feedback until you indicate end-of-file by typing CTRL-z, at which point it will process what you typed and most likely tell you about all the errors you made. If you want line-by-line interactive feedback, use irb.

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.

Why won't macvim always use ruby 1.9.3?

I have installed yadr dotfiles, a set of vim, ruby, etc plugins.
I have the following line of Ruby code in a file foo.rb:
foo: bar
Note I used the ruby 1.9.3 syntax for symbol assignment/definition.
When I start macvim from command line using mvim foo.rb and save that file, everything works fine.
However, when I open macvim using open -a macvim and navigate to and open foo.rb, when I try to save the file I get a ruby-vim syntax error on foo: bar. When I change it to :foo => bar I don't get syntax errors.
Using open -a macvim to open macvim, and then entering :!ruby -v prints ruby 1.8.7
Using mvim . to open macvim, and then entering :!ruby -v prints ruby 1.9.3
Depending on how I open macvim, I get a different version of Ruby. How do I ensure that macvim always uses ruby 1.9.3 to evaluate my ruby code?
Thanks
It took me awhile to find a fix, but the issue is caused by MacVim not loading zsh the same way Terminal loads zsh.
The fix is easy enough and can be placed into your zshrc. See a commit from my dotfiles:
https://github.com/simeonwillbanks/dotfiles/commit/e0e19cfeff13f8bc99d8164217ddd84c6d7f9529
The commit references a full explanation which can be found here:
http://vim.1045645.n5.nabble.com/MacVim-and-PATH-tt3388705.html#a3392363
Hope this helps!

rvm-installed ruby: runs in terminal fine, not anywhere else

I have installed Ruby via RVM on Linux Mint 11. It seems to have installed fine: when I enter type rvm | head -1, I get "rvm is a function" back. Entering ruby -v gives me 1.9.2p290. I can run ruby scripts from the bash terminal window fine. However, when I try to run the same scripts from say gvim (I've got a shortcut mapped to "ruby ") or geany or gedit (ditto), I get "ruby: command not found" (in gvim) or "ruby: not found" (in geany or gedit).
Here's more information: "which ruby" gives me: $HOME/.rvm/rubies/ruby-1.9.2-p290/bin/ruby. $HOME/.rvm/rubies/ruby-1.9.2-p290/bin is in my path in my .bashrc. On another Linux Mint 11 machine, where all works fine, "which ruby" gives me $HOME/.rvm/bin/ruby. Also, on the machine where all is fine, I have a $HOME/bin folder that I don't know where it came from, but was created the same day as my .rvm folder. It contains among other things, links to shell scripts in the $HOME/.rvm/wrappers folder.
I had same problem, you should try making shell login. To make so use the command "bash -l" instead of simply "bash". In geany you can find where to change it in edit->preferences->terminal->shell
I think you dont have your current ruby set as system default. try running this command:
rvm use 1.9.2 --default

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 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.

Resources