How to enforce Ruby code to break long lines with multiple arguments into one argument per line

preview_player
Показать описание

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!How to enforce Ruby code to break long lines with multiple arguments into one argument per line

I'm currently using RuboCop in a Ruby on Rails project, and I have a question regarding code style enforcement for long lines with multiple arguments. We have set a line length maximum of 100 characters, but we're running into inconsistencies when dealing with method calls that have many arguments.
For example, both of the following code snippets are accepted by RuboCop:
create(:user, first_name: 'Bartolomeo', last_name: 'Ruperlstickly',
phone: '+1234455678', address: '1234 str.')

create(:user, first_name: 'Bartolomeo', last_name: 'Ruperlstickly',
phone: '+1234455678', address: '1234 str.')

create(:user,
first_name: 'Bartolomeo',
last_name: 'Ruperlstickly',
phone: '+1234455678',
address: '1234 str.')

create(:user,
first_name: 'Bartolomeo',
last_name: 'Ruperlstickly',
phone: '+1234455678',
address: '1234 str.')

What we would like to enforce is that if the arguments can't fit on one line (due to our 100 character limit), they should be broken into one argument per line, as per the latter option.
I've tried adjusting RuboCop settings, but I haven't been able to enforce this style strictly. Is there a way to configure RuboCop to enforce this rule? Or do I need to use an additional tool or plugin to achieve this level of formatting consistency?
Layout/FirstParameterIndentation:
EnforcedStyle: consistent
IndentationWidth: 2

Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation

Layout/FirstParameterIndentation:
EnforcedStyle: consistent
IndentationWidth: 2

Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation

While this helps with indentation, it doesn’t enforce breaking the arguments into separate lines when they exceed the max line length.
I could write a custom cop to enforce this (I think?) but I was hoping there was already a Rubocop rule I'm overlooking or a different gem.
Overall, I would like it to work like prettier does in JS, where you unequivocally always have a single way of breaking a method into multiple lines, so that nobody in the project has to think about it. I'm not closed to using Rubocop; If there's another gem that would do this for us I'm all in, since it would save our team a lot of nitpick style comments on PRs.
Thank you!

Tags: ruby,rubocopSource of the question:

Question and source license information:
Рекомендации по теме
welcome to shbcf.ru