Coding Style Checker or Code Formatter for Ruby/Rails

Coding style checker or code formatter for Ruby / Rails

There's some style checkers listed at in the Ruby toolbox at https://www.ruby-toolbox.com/categories/code_metrics .

Also, turning on warnings can check for some kinds of bad code. Do so with $VERBOSE = true or by one of the ways listed here.

Are there any gems which can enforce Ruby code conventions?

My two favorite gems for handling code cleanliness are Reek (https://github.com/troessner/reek) and Cane (https://github.com/square/cane). I don't know that they go down to the nitty-gritty of indentation and spacing, but they detect a lot of Ruby coding conventions that tend to otherwise get overlooked.

In terms of spacing / indentation / casing and so forth, I'd either look for an editor configuration (e.g., download a Ruby package for TextMate / Sublime Text / EMACS / what have you), or look into the BeautifyRuby gem (https://github.com/CraigWilliams/BeautifyRuby).

Ruby Source code Auto-Formatter

There is RuboCop:

gem install rubocop
rubocop -a

It will also fix all the common mistakes.

What tools are available to auto-format Ruby/Rails Code

It's not new but it works:
http://www.arachnoid.com/ruby/rubyBeautifier.html

How do I auto format Ruby or .erb files in VS Code?

You can set format associations in VSCode, so .erb files would be treated like .html.

Go to File->Preferences->Settings->Click ... in top right corner->Open settings.json

Then add this piece of code to your settings.json

"files.associations": {
"*.html.erb": "html"
}

This is how I solved this problem. It will remove some of the code highlights but will autoformat HTML templates like an HTML document.

Where can I find an actively developed lint tool for Ruby?

You could give Diamondback Ruby a try. It does a static typecheck of Ruby code, and will thus blame you for using an undefined variable.

While DRuby is an ongoing research project, it already works quite well for small, self-contained Ruby scripts. Currently, it is unable to analyze much of the Ruby standard library “out-of-the-box”. Currently they are working toward typing Ruby on Rails (see their most recent papers).



Related Topics



Leave a reply



Submit