-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Ruby Styleguide
We generally follow the Ruby Style Guide maintained by bbatsov, with the following exceptions:
It is acceptable to use !!
to coerce a value to be true
or false
.
It is acceptable to have a comma after the last item of an Array
or Hash
literal.
raise
is acceptable to use instead of fail
.
Stabby lambdas (->
) are preferred for both single line and multi-line blocks. They solve a syntactic ambiguity with pipes being used in defaults for arguments.
Limit lines to 120 characters.
Ruby allows several special variables which it primarily inherited from Perl. These are a $ followed by one other character. Avoid their use, except for the following, as they are better known than any alternative:
-
$?
which returns Process::Status for last executed child process (e.g., from ls -l). -
$!
which returns the pending exception. This is the second parameter to a raise statement.
Use named captures and Regexp#match rather than Perl variables ($~
, $1
). See Refactoring Regular Expressions with Ruby 1.9 Named Captures.
Note that =~ is allowed when you don't have any captures. Regexp.last_match is not allowed.
While a loop … break construction is preferred, post-loop conditionals are acceptable.
While we prefer single-line while/until (https://github.com/bbatsov/ruby-style-guide#while-as-a-modifier), use common sense if the line length gets too long. Break it into a multi-line loop.
Prefer normal indent over indenting to the first paren:
# bad
def this_is_especially_bad_with_long_method_names(first_attribute: variable_foo,
second_attribute: variable_bar,
third_attribute: variable_baz)
end
# good (normal indent)
def send_mail(source)
Mailer.deliver(
to: '[email protected]',
from: '[email protected]',
subject: 'Important message',
body: source.text
)
end
No one likes to wrangle whitespace. Ref: https://github.com/bbatsov/ruby-style-guide#no-double-indent
Are you looking for one of our commercial subscriptions, professional services, support, or our hosted solution? Check out canvaslms.com.