Enforce first hash and array element to be on the same line in rubocop

I'm looking for a way to enforce this way of writing hashes and arrays using rubocop:

a = { first: 'a',
      second: 'b' }

I found rules to enforce closing brace and indentation. But can't enforce the first element to be inline with an opening brace. Is it possible?

So far I found Style/FirstHashElementLineBreak , but it does the opposite.

To force the closing brace to be on the same line as the last key I use this:

Style/MultilineHashBraceLayout:
  EnforcedStyle: same_line

But this setting kind of not make sense without forcing the opening brace to be on the same line as the first key.

I'm asking people who ever tried to do similar thing, or who have a good knowledge of rubocop's configuration, is it possible to do so, and if it is, how to do it?


First off, the Style/MultilineHashBraceLayout has been renamed Layout/MultilineHashBraceLayout and second the default config for it now symmetrical does exactly what you want.

From the docs:

When using the symmetrical (default) style:

If a hash's opening brace is on the same line as the first element of the hash, then the closing brace should be on the same line as the last element of the hash.

If a hash's opening brace is on the line above the first element of the hash, then the closing brace should be on the line below the last element of the hash.

So running rubocop --only Layout/MultilineHashBraceLayout on the code above will yield now offenses.

链接地址: http://www.djcxy.com/p/60962.html

上一篇: MySQL Connector / Net向后兼容?

下一篇: 强制第一个散列和数组元素位于rubocop中的同一行上