Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding style checker or code formatter for Ruby / Rails

When I use C# or Perl, there are some useful tools like StyleCop, FxCop, Perl::Critic and Perltidy. They can check or format my code automatically. Then, are there any equivalent tools for Ruby or Rails? I found some tools on Google, but I felt they are not maintained so frequently.

like image 304
Junichi Ito Avatar asked Feb 01 '12 21:02

Junichi Ito


People also ask

What is RuboCop in Ruby?

Overview. RuboCop is a Ruby code style checker (linter) and formatter based on the community-driven Ruby Style Guide. RuboCop is extremely flexible and most aspects of its behavior can be tweaked via various configuration options.

Does prettier work with Ruby?

How does it work? Prettier for Ruby works through Prettier's plugin API. Its parse function works by spawning a Ruby process and using Ruby's own parser (known as Ripper ).

What is Linting in Ruby?

Linting is the automated checking of source code for programmatic and stylistic errors. This checking is performed by a static code analysis tool called a linter. A code formatter, however, is a tool concerned with formatting source code so that it strictly adheres to a pre-configured set of rules.


Video Answer


1 Answers

I have tried two different tools: rubocop and Cane. In a test I found that rubocop gives almost twice as many warnings as cane (and rubocop reports everything that Cane reports). So, I recommend rubocop.

rubocop

rubocop is a

Ruby code style checker based on the Ruby Style Guide.

Installation

You can install it as a gem:

gem install rubocop

(The above might need to prefixed the command with sudo depending on your setup.)

Usage

rubocop is used by running the command with the same name from the command line. If you provide files as arguments rubocop checks those files. If you provide directories as arguments rubocop check Ruby files in the directories recursively, i.e. in the given directories and all their subdirectories. If you provide no argument it check the working directory recursively.

Here is an example. Given the following file

say="I am not in style"
puts(say)

['cow','cat','cake'].each{|i| print i+' '}

robucop reports as follows:

$ rubocop ~/test/rubocop.rb
== /home/nn/test/rubocop.rb ==
C:  1: Missing encoding comment.
C:  1: Surrounding space missing for operator '='.
C:  4: Surrounding space missing for operator '+'.
C:  4: Surrounding space missing for '{'.
C:  4: Space missing to the left of '}'.
C:  4: Space missing after comma.
C:  4: Space missing after comma.
C:  1: Prefer single-quoted strings when you don't need string interpolation or special symbols.

1 files inspected, 8 offences detected

Note that rubocop can output warnings that Emacs can parse via the -e option.

Cane

Cane is a tool to check code style. It can be integrated with Rake.

Installation

You can install it as a gem:

gem install cane

(The above might need to prefixed the command with sudo depending on your setup.)

Usage

To use it run cane on the directories or files you want to check.

like image 89
N.N. Avatar answered Sep 28 '22 15:09

N.N.