Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 927480e

Browse files
koicbbatsov
authored andcommitted
Add new "Single-line do...end block" rule
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) } ```
1 parent 0c97b36 commit 927480e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,6 +2618,27 @@ names.select { |name| name.start_with?('S') }.map(&:upcase)
26182618

26192619
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?
26202620

2621+
=== Single-line `do`...`end` block [[single-line-do-end-block]]
2622+
2623+
Use multi-line `do`...`end` block instead of single-line `do`...`end` block.
2624+
2625+
[source,ruby]
2626+
----
2627+
# bad
2628+
foo do |arg| bar(arg) end
2629+
2630+
# good
2631+
foo do |arg|
2632+
bar(arg)
2633+
end
2634+
2635+
# bad
2636+
->(arg) do bar(arg) end
2637+
2638+
# good
2639+
->(arg) { bar(arg) }
2640+
----
2641+
26212642
=== Explicit Block Argument [[block-argument]]
26222643

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

0 commit comments

Comments
 (0)