Skip to content

Commit

Permalink
Add new "Single-line do...end block" rule
Browse files Browse the repository at this point in the history
Follow up rubocop/rubocop#12227 (comment)

This PR adds new "Single-line `do`...`end` block" rule.

Use multi-line `do`...`end` block instead of single-line `do`...`end` block.

```ruby
# bad
foo do |arg| bar(arg) end

# good
foo do |arg|
  bar(arg)
end

# bad
->(arg) do bar(arg) end

# good
->(arg) { bar(arg) }
```
  • Loading branch information
koic committed Sep 29, 2023
1 parent eb09cfa commit 9719513
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,27 @@ names.select { |name| name.start_with?('S') }.map(&:upcase)

Some will argue that multi-line chaining would look OK with the use of {...}, but they should ask themselves - is this code really readable and can the blocks' contents be extracted into nifty methods?

=== Single-line `do`...`end` block [[single-line-do-end-block]]

Use multi-line `do`...`end` block instead of single-line `do`...`end` block.

[source,ruby]
----
# bad
foo do |arg| bar(arg) end
# good
foo do |arg|
bar(arg)
end
# bad
->(arg) do bar(arg) end
# good
->(arg) { bar(arg) }
----

=== Explicit Block Argument [[block-argument]]

Consider using explicit block argument to avoid writing block literal that just passes its arguments to another block.
Expand Down

0 comments on commit 9719513

Please sign in to comment.