rubocop / rubocop/ruby-style-guide

Empty line around nesting blocks of the same indentation

Open
#823 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
16.5k
Forks
3.3k
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing style-guide guidance on empty lines, nesting contexts, and blocks. Check the issue discussion for a decision on this proposed rule before changing anything. Done means documenting an agreed rule with consistent good and bad examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.