text
stringlengths 0
444
|
|---|
Adopt a consistent multi-line method chaining style.
|
There are two popular styles in the Ruby community, both of which are considered good - leading `.` and trailing `.`.
|
==== Leading `.` [[leading-dot-in-multi-line-chains]]
|
When continuing a chained method call on another line, keep the `.` on the second line.
|
[source,ruby]
|
----
|
# bad - need to consult first line to understand second line
|
one.two.three.
|
four
|
# good - it's immediately clear what's going on the second line
|
one.two.three
|
.four
|
----
|
==== Trailing `.` [[trailing-dot-in-multi-line-chains]]
|
When continuing a chained method call on another line, include the `.` on the first line to indicate that the expression continues.
|
[source,ruby]
|
----
|
# bad - need to read ahead to the second line to know that the chain continues
|
one.two.three
|
.four
|
# good - it's immediately clear that the expression continues beyond the first line
|
one.two.three.
|
four
|
----
|
A discussion on the merits of both alternative styles can be found https://github.com/rubocop/ruby-style-guide/pull/176[here].
|
=== Method Arguments Alignment [[no-double-indent]]
|
Align the arguments of a method call if they span more than one line.
|
When aligning arguments is not appropriate due to line-length constraints, single indent for the lines after the first is also acceptable.
|
[source,ruby]
|
----
|
# starting point (line is too long)
|
def send_mail(source)
|
Mailer.deliver(to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', body: source.text)
|
end
|
# bad (double indent)
|
def send_mail(source)
|
Mailer.deliver(
|
to: 'bob@example.com',
|
from: 'us@example.com',
|
subject: 'Important message',
|
body: source.text)
|
end
|
# good
|
def send_mail(source)
|
Mailer.deliver(to: 'bob@example.com',
|
from: 'us@example.com',
|
subject: 'Important message',
|
body: source.text)
|
end
|
# good (normal indent)
|
def send_mail(source)
|
Mailer.deliver(
|
to: 'bob@example.com',
|
from: 'us@example.com',
|
subject: 'Important message',
|
body: source.text
|
)
|
end
|
----
|
=== Implicit Options Hash [[no-braces-opts-hash]]
|
IMPORTANT: As of Ruby 2.7 braces around an options hash are no longer
|
optional.
|
Omit the outer braces around an implicit options hash.
|
[source,ruby]
|
----
|
# bad
|
user.set({ name: 'John', age: 45, permissions: { read: true } })
|
# good
|
user.set(name: 'John', age: 45, permissions: { read: true })
|
----
|
=== DSL Method Calls [[no-dsl-decorating]]
|
Omit both the outer braces and parentheses for methods that are part of an internal DSL.
|
[source,ruby]
|
----
|
class Person < ActiveRecord::Base
|
# bad
|
validates(:name, { presence: true, length: { within: 1..10 } })
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.