Manual:Coding conventions/Ruby

This article describes the coding conventions for Ruby files of MediaWiki related codebases.

Unlike Python with its PEP 8, Ruby has no truly canonical style guide to which to reference. There are, however, a few well respected guides that can serve as reference points for Rubyists, most prominently those from GitHub, Heroku, and the community-driven guide from bbatsov.

Starting point
For reasons both public-spirited and practical, we have adopted the bbatsov guide as a starting point; it's the most participatory of the three and comes with the rubocop code analyzer that can be easily customized and integrated into our continuous integration.

Exceptions
There are a handful of areas where the bbatsov guide attempts to draw the line with seemingly arbitrary limits or where there's quite a bit of disagreement among commenters. In most of these cases, we've opting to simply ignore the rules and defer in good faith to the developer.

Exceptions and additions to the bbatsov guide are enumerated herein.

Method length

 * RuboCop ID :
 * Enforced : no

In general, developers should follow the single responsibility principle, but the bbatsov guide's 10-line limit seems rather arbitrary.

Line length

 * RuboCop ID :
 * Enforced : yes, but at a 100 character limit

It's suggested in the bbatsov guide that lines should never exceed 80 characters in length, but there's an ongoing debate over it. In fact, statistical analysis of the ten most popular Ruby projects on GitHub shows that a limit of 80 would invalidate more than 8% of existing lines while a more liberal limit of 100 or 120 would only invalid around 3% and 1% respectively. Falling back on our own general coding conventions, 100 lines will be our current limit.

Multi-line method chaining

 * RuboCop ID :
 * Enforced : per project

Two options are presented in the bbatsov guide. We allow either form as long as you stay consistent within each project.

Signaling exceptions

 * RuboCop ID :
 * Enforced : no

The bbatsov guide suggests using  to signal new exceptions and   to re-raise rescued ones. However, there's some objection to the rule based on the lack of the former in existing Ruby projects or prominent Ruby literature, and the contrived nature of useful semantic differences given as a justification. For now, we don't enforce this rule.

Aliasing methods

 * RuboCop ID :
 * Enforced : no

The bbatsov guide suggests that  should always be used over. However, there are cases where  can be just as clear or even more appropriate when written alongside methods defined with.

In short, it's ok to use  within the body of a class or module definition to alias a method also defined therein, but it should not be used to alias methods defined by a macro;   is more appropriate in that case since it's a macro itself.

RuboCop
RuboCop check is enabled for MediaWiki and related repos that contain Ruby code, enforcing the conventions outlined here. In most cases, a configuration file has been initialized using the  option; the generated config will ignore all violations by default, allowing maintainers to address them one by one.

Ignoring or customizing RuboCop rules
When you want to ignore a rule or customize its behavior, whether you're going by the recommendations in this guide or not, you'll need to add some configuration to the  file in the project's root directory (after the   entry).

For example, to ignore the  rule altogether, you'd add the following.

To customize it to using a more liberal line length limit, say 120, you'd add this.

Please consult the RuboCop documentation for possible rule names and options.

Base configuration
Following is a RuboCop configuration that reflects the exceptions above. Please use it as a starting point for your Ruby projects.