Daniel
Daniel

Reputation: 4321

Allow braces for blocks that return

When I write a multi-line block that returns a value, I like to use braces instead of do-end in order to make evident that the purpose of the block is to return things, as opposed to do things.

The standard configuration of Rubocop doesn't allow that, and rubocop -a converts any multi-line block in a do-end block.

# I like but Rubocop hates.
expect {
  delete :destroy, id: thing.to_param
}.to change(Thing, :count).by(-1)

# I don't like but Rubocop does.
expect do
  delete :destroy, id: thing.to_param
end.to change(Thing, :count).by(-1)

# That, we both like.
before do
  stub_api_calls
  admin_sign_in create(:super_admin)   
end

Is there any configuration parameter in Rubocop that allows this distinction?

Edit: Or, at least, that avoids changing the brackets block if there is a dot (.) after the closing bracket.

Upvotes: 2

Views: 1613

Answers (1)

Daniel
Daniel

Reputation: 4321

I discovered that there's a Cop named Style/BlockDelimiters that does the trick. Configured as semantic, it enforces curly braces around functional blocks and do..end around imperative code.

This is what I added to .rubocop.yml:

# Allow curly braces for functional blocks
BlockDelimiters:
  EnforcedStyle: semantic

Upvotes: 2

Related Questions