ruby-style-guide
ruby-style-guide copied to clipboard
Empty line around nesting blocks of the same indentation
I was a bit surprised with a colleague today when I learned that this was allowed by default:
def foo
numbers = [1,2,3]
numbers.map do |n|
n+1
end
end
as I don't allow myself to do that. There would be an empty line before the map block.
The logic, which is consistent with some other rules:
- There must be an empty line between a block that creates a new nesting context and something else at the same indent level.
(A nesting context being something that has different indentations inside of it)
Meaning:
# good
def foo
numbers = [1,2,3]
numbers.map do |n|
n+1
end
end
# bad
def foo
numbers = [1,2,3]
numbers.map do |n|
n+1
end
end
# bad: indent level changes, thus empty line is not needed
def foo
numbers = [1,2,3]
numbers.map do |n|
n+1
end
end
# bad
def foo
numbers = [1,2,3]
numbers.map! do |n|
n+1
end
numbers.map do |n|
n+2
end
end
I apply this logic to any nesting context: modules, methods, blocks...
Have I missed something?
@joallard It seems rubocop allows nested blocks without newlines by default. I am facing this issue too but couldn't find a solution for it.
Same. I was surprised this style is not enforced by default...I could have sworn there was from my early Ruby days, but it seems I misremember. Further surprised there is no built-in option for it. Also surprised there is no extension to do this. Please reply here if there is one.
Thank you for the support. After 3 years without comments to the contrary, it seems like it would make sense to move on to the next step and propose adding it to the style guide.