Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty line around nesting blocks of the same indentation #823

Open
joallard opened this issue Jul 2, 2020 · 3 comments
Open

Empty line around nesting blocks of the same indentation #823

joallard opened this issue Jul 2, 2020 · 3 comments

Comments

@joallard
Copy link

joallard commented Jul 2, 2020

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?

@eslamodeh
Copy link

@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.

@dhempy
Copy link

dhempy commented Feb 7, 2023

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.

@joallard
Copy link
Author

joallard commented Mar 8, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@joallard @dhempy @eslamodeh and others