$ rubocop -V
0.68.1 (using Parser 2.5.3.0, running on ruby 2.5.3 x86_64-linux)
When writing the following code
class Foo
def bar
'baz'
end
end
I would expect the code to look like this after running Rubocop:
class Foo
def bar
'baz'
end
end
Instead nothing changes. The wrong indentation of the "end" keyword of the "bar" method stays at the same position.
Rubocop is running. Other offenses get corrected.
Is this expected behaviour?
Thanks!
If I recall correctly that particular autocorrect is disabled by default, as customary for Lint cops.
@bbatsov thank you very much for the fast reply :nerd_face:
Could you maybe provide a link to the docs where to enable it?
Thanks!
Seems it's this one http://www.rubocop.org/en/latest/cops_layout/#layoutdefendalignment And my bad - it's not a lint cop. Not sure why we disabled the auto-correct then.
Shouldn't it be the default "align with def" and also be auto-corrected?
Actually never seen Ruby code like this:
# good
private def foo
end
isnt' the convention to write the private keyword on it's own line?
Like this ( and usually indenting the following private methods):
private
def foo
end
What's your opinion about that? Are there any reasons why Rubocop doesn't handle it this way?
Thanks!
The examples are a bit misleading, as they cover only the difference in the two alignment styles in the presence of some macro before the def. In the absence of modifiers it will just align with the def.
Ah ok I understand. So you got no idea why the auto-correct is disabled by default then?
Aligning end with def (or another element) is always wanted I and doesn't alter anything in the code's semantics so it's always safe I guess...
Ah ok I understand. So you got no idea why the auto-correct is disabled by default then?
Likely it's because we moved the cop around departments. Don't have the time to check this now. Lots have happened and I've started to forget things. :-) I think originally we didn't want to align def automatically because this could mask some logic mistake - when the code was written with the wrong indentation someone believe they were in one scope, but they were actually in a different scope. Someone else on the team might remember the story better.
Thanks for the answer, that makes quite a lot of sense to me. No further explanations needed. Just have been curious why decisions were made the way they are. :-) Have a nice day! :-)