How can I get Syntastic to recognize newer Ruby syntax?

10

2

I'm using Syntastic in Vim, and it highlights newer Ruby syntax as errors. For example, if I use required keyword arguments (introduced in Ruby 2.1), like this:

def distance(from:, to:)
  # whatever
end

... it highlights the definition line and says "unexpected ','".

How can I get it to recognize this as valid Ruby syntax? I've already updated ~/.vim/syntax/ruby.vim.

Nathan Long

Posted 2015-02-06T13:56:43.543

Reputation: 20 371

Note: ~/.vim/syntax/ruby.vim has nothing to do with checking syntax in files; it's for syntax highlighting. See http://robots.thoughtbot.com/writing-vim-syntax-plugins

– Nathan Long – 2015-02-16T14:13:11.040

Answers

9

Specify the ruby executable for Syntastic

Syntastic calls ruby -c to check the syntax of a file, so the errors it shows depends on which version of ruby its using.

You can point it to the one you want to use like this:

let g:syntastic_ruby_exec = 'path/to/ruby/executable'

NOTE: on newer versions of Syntastic:

let g:syntastic_ruby_mri_exec = 'path/to/ruby/executable'

$ type ruby will tell you where your current Ruby executable is. Ruby installers have their own conventions about where they place their binaries. Eg:

  • ruby-install puts ruby installations in ~/.rubies and specific versions at paths like ~/.rubies/ruby-2.2.0/bin/ruby
  • RVM puts ruby installations in ~/.rvm/rubies and specific versions at paths like ~/.rvm/rubies/ruby-2.2.0/bin/ruby

Nathan Long

Posted 2015-02-06T13:56:43.543

Reputation: 20 371

FYI: if you are using rvm it would be let g:syntastic_ruby_exec = '~/.rvm/rubies/ruby-2.2.0/bin/ruby' – Ben – 2015-02-15T19:05:55.710

@Ben good point - updated answer – Nathan Long – 2015-02-16T13:57:24.603

3For the newer version of syntastic, it's: let g:syntastic_ruby_mri_exec='path/to/ruby/executable' – RantriX – 2015-05-15T02:15:17.830

1If you're using rbenv you should be able to do let g:syntastic_ruby_exec = '~/.rbenv/shims/ruby' so that when ruby versions switch it should automatically pick it up, I think. – Thermatix – 2016-07-22T14:51:31.467

Nice @Thermatix.. I used system("rbenv which ruby") in my .vimrc – rthbound – 2016-12-30T23:35:06.097

4

Using terminal macvim I also had this problem, which was compounded by the fact that I was using zsh, which for some reason was not respecting the rvm binary I gave syntastic. I realized that my paths where incorrect when running !echo $PATH in terminal macvim, I had several system paths appended to the top that were not in my regular shell still causing the default MRI to load.

To fix this, I did not need to specify the syntax checker at all in my .vimrc, however I had to update how zsh started. Using the thread at: http://vim.1045645.n5.nabble.com/MacVim-and-PATH-td3388705.html I ended up simply running sudo mv /etc/zshenv /etc/zprofile and now everything works and my paths are correct in mvim. This should also solve the problem of updating syntastic when installing a new ruby version via rvm.

Adam

Posted 2015-02-06T13:56:43.543

Reputation: 246

This is the actual fix! The problem is because of a caveat with zsh installed via brew in OSX.

– Jikku Jose – 2015-07-11T05:46:55.550

Random question - isn't "terminal macvim" just vim? What benefits do you gain using Macvim in the terminal over using plain vim? – Nick – 2015-12-01T16:33:48.717

0

For a Rails project.

  • Set the ruby version in a .ruby-version file at project root.
  • And start your vim from the project root.

Syntastic will do the syntax check with the specified ruby version.

lingceng

Posted 2015-02-06T13:56:43.543

Reputation: 101