10

I've been having this problem for a while with vim. The first time I do anything that interacts with ruby, such as :ruby puts "test", I get <internal:gem_prelude>:1:in 'require': cannot load such file -- rubygems.rb (LoadError).

Some diagnostic information that may be useful : my OS is OS X 10.11.2, Vim is version 7.4, ruby is 2.1.2 installed with rvm, my shell is zsh (but this also happens with bash), and my vim is completely vanilla.

 $  ruby --version
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
 $  rvm list

rvm rubies

=* ruby-2.1.2 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

 $  which -a ruby
/Users/marcusbuffett/.rvm/rubies/ruby-2.1.2/bin/ruby
/usr/local/bin/ruby
/usr/local/bin/ruby
/usr/bin/ruby
 $  vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Mar 18 2016 09:07:16)
MacOS X (unix) version
Included patches: 1-1401
Compiled by Homebrew
Huge version without GUI.  Features included (+) or not (-):
+acl             +farsi           +mouse_netterm   +syntax
+arabic          +file_in_path    +mouse_sgr       +tag_binary
+autocmd         +find_in_path    -mouse_sysmouse  +tag_old_static
-balloon_eval    +float           +mouse_urxvt     -tag_any_white
-browse          +folding         +mouse_xterm     -tcl
++builtin_terms  -footer          +multi_byte      +terminfo
+byte_offset     +fork()          +multi_lang      +termresponse
+channel         -gettext         -mzscheme        +textobjects
+cindent         -hangul_input    +netbeans_intg   +title
-clientserver    +iconv           +packages        -toolbar
+clipboard       +insert_expand   +path_extra      +user_commands
+cmdline_compl   +job             +perl            +vertsplit
+cmdline_hist    +jumplist        +persistent_undo +virtualedit
+cmdline_info    +keymap          +postscript      +visual
+comments        +langmap         +printer         +visualextra
+conceal         +libcall         +profile         +viminfo
+cryptv          +linebreak       +python          +vreplace
+cscope          +lispindent      -python3         +wildignore
+cursorbind      +listcmds        +quickfix        +wildmenu
+cursorshape     +localmap        +reltime         +windows
+dialog_con      -lua             +rightleft       +writebackup
+diff            +menu            +ruby            -X11
+digraphs        +mksession       +scrollbind      -xfontset
-dnd             +modify_fname    +signs           -xim
-ebcdic          +mouse           +smartindent     -xsmp
+emacs_tags      -mouseshape      -sniff           -xterm_clipboard
+eval            +mouse_dec       +startuptime     -xterm_save
+ex_extra        -mouse_gpm       +statusline      -xpm
+extra_search    -mouse_jsbterm   -sun_workshop
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/local/share/vim"
Compilation: /usr/bin/clang -c -I. -Iproto -DHAVE_CONFIG_H   -F/usr/local/Frameworks -DMACOS_X_UNIX  -Os -w -pipe -march=native -mmacosx-version-min=10.11 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: /usr/bin/clang   -L. -L/Users/travis/.sm/pkg/active/lib -fPIC -Bstatic -fstack-protector -L/usr/local/lib -F/usr/local/Frameworks -Wl,-headerpad_max_install_names -o vim        -lm  -lncurses -liconv -framework Cocoa   -fstack-protector  -L/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE -lperl -framework Python   -lruby-static -framework CoreFoundation -lobjc -L/Users/marcusbuffett/.rvm/rubies/ruby-2.1.2/lib
Marcus Buffett
  • 1,289
  • 1
  • 14
  • 32

3 Answers3

2

Assuming that ruby works for you normally when using from the command line, I suspect that the problem comes from the fact that you are trying to use a RVM ruby in VIM and VIM doesn't use a login shell by default. RVM, on the other hand has it's initialization done in the shell init scripts, which are run only in login shells, which you get e.g. when you open a terminal.

Now, as you have also a system ruby installed in /usr/local, VIM sees this system ruby instead of the RVM one. And I guess you don't have rubygems installed in the system ruby.

So, one quick method is to force VIM to use the login shell, so that it behaves the same way as your command line. This is answered in this SO question, and I quote from there:

# ~/.vimrc
set shell=bash\ -l

Another option would be to manually set the VIM PATH so that the first found ruby is the one from RVM, not the system one.

Community
  • 1
  • 1
Matouš Borák
  • 15,606
  • 1
  • 42
  • 53
  • 1
    Tried this, unfortunately didn't work. When I do `:ruby puts RUBY_VERSION`, I get the ruby I have installed with RVM (2.1.2), so that doesn't seem to be the problem. – Marcus Buffett Mar 24 '16 at 16:04
  • Wait a minute, are you saying that `:ruby puts RUBY_VERSION` works but `:ruby puts "test"` does not? – Matouš Borák Mar 24 '16 at 19:47
  • 1
    It's always the first ruby command that fails, after that it all seems to work, so the first `:ruby puts RUBY_VERSION` will show the error, the second will give me 2.1.2 – Marcus Buffett Mar 24 '16 at 20:42
  • 1
    same issue here, first command fails, second executes well ( no answer? – Pavel Apr 07 '16 at 01:19
  • 1
    I'm on a Mac (OS X Yosemite). I used to have this problem when using vim 7.4, which was installed via brew. After uninstalling vim 7.4, and returning back to vim 7.3 (system default), the problem went away for me. – Chris Aug 11 '16 at 15:59
  • @MarcusBuffett Did you find any workaround to the problem? Only the first ruby command fails for me as well. – Sankha Narayan Guria Sep 24 '16 at 11:01
0

You could just run ruby like any other command with :!ruby -e "puts 'Hello'", or -- to run the current file -- :!ruby %:p.

Caveat: This will, by itself, toggle back to your shell until you press a key to go back to vim, as opposed to executing the code in the command window like :ruby does. I consider it worth the tradeoff, considering that it is significantly more versatile. See below for a workaround.


The last one can be particularly useful when paired with:

if __FILE__ == $0
  run_some_command || assert_something || run_only_this_test_file
end

You can, for example, add an autocmd on buffer write to specific file names (e.g. *_test.rb) that automatically runs a test on save, if that sort of thing interests you.


Alternative to Screen Flickering on Shell Command

If the flickering annoys you, you can also store the result into a variable, spawn a new buffer and put the result in it with :let res=system('ruby '.expand('%:p')) | new | put=res

0

You could try

rvm fix-permissions && rvm reinstall 2.1.2

I think I saw this on stackoverflow before as a correct answer for a similar non-vim issue.

Nakul Pathak
  • 109
  • 1
  • 1
  • 9