19

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.

3
  • possible duplicate of Where can I find an actively developed lint tool for Ruby? Commented Feb 1, 2012 at 22:04
  • I did not notice it. I will check it later. Thank you. Commented Feb 2, 2012 at 9:13
  • Good question, but I think you accepted wrong answer. Rubocop/Cane is closer to question than code metrics tools listed on rubytoolbox. Commented Feb 21, 2013 at 17:36

3 Answers 3

25

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.

Sign up to request clarification or add additional context in comments.

Comments

11

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.

Comments

1

As for code checkers, http://ruby.sadi.st/Ruby_Sadist.html is a good collection of libraries. Worth mentioning in addition to the ruby toolbox since they're often used together (and written by the same group of people).

As for formatters, every so often I go looking to see if any have come up, but the answer is still no. Other than auto-indenting, which any worthy editor can do or has a plugin for already, no formatters exist to my knowledge. I've heard people attribute it to ruby being hard to parse, but ruby parsers do exist, so who knows why this is.

1 Comment

Thank you, I will check them later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.