New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new Style/ClassMethodsDefinitions
cop
#8381
Add new Style/ClassMethodsDefinitions
cop
#8381
Conversation
There's also a third option - using the class name, but we might have a cop about this already, as it's definitely bad style.
Same here. It produces the easiest to read code. I'm surprised we didn't add anything to our style guide on the topic.
Great ideas! The first one should probably cover empty class/module definitions in general. |
FWIW, I might write
Not directly related, but I love |
@fatkodima ping :-) |
Working on this. Will update shortly. |
b7ad526
to
3f2c2d3
Compare
Enabled this cop on |
Looking at the diff it seems we shouldn't report offenses when there's a mixture of method definitions and something else in |
For Report: class << self
def do_something
end
attr_reader :foo
end Do not report: class << self
def do_something
end
attr_reader :foo
private
def do_something_else
end
end |
Doesn't this contradict the rest of what you wrote? |
Mmm, haven't seen any? I'm proposing a change that, when class << self
def do_something
end
attr_reader :foo
end should be rewritten as def self.do_something
end
class << self
attr_reader :foo
end And in the following case class << self
def do_something
end
attr_reader :foo
private
def do_something_else
end
end no offense should be triggered. Am I missing something? |
No, my bad, sorry for the noise 🤦♂️ |
IMHO, I'm worried about enforcing to only either style. This is because it is doubt that decomposing the intentionally grouped |
Is there already an issue about this in the style-guide? I can't seem to find it. |
Don't think so. Feel free to create one.
@koic I had suggested enabling this for RuboCop itself, not for the end users. But I agree we probably need to discuss it a bit more, as I didn't consider the cases where there something besides methods in the singleton class. |
05488b9
to
c6712b7
Compare
Updated:
|
Thank you for your consideration. Even in RuboCop's codebase, I think it's preferable to disable it by default. This is because there are some cases where code before the change is preferable. |
Fair enough, let's keep it disabled for RuboCop as well and revert the changes related to it. |
c6712b7
to
62ef486
Compare
Made it disabled and reverted style changes. |
Thanks! |
This cop enforces how class methods are defined:
I have checked several existing style guides, and found that:
So seems like people prefer to use either one of them. Personally I prefer
def self.method
style and noticed thatrubocop
s codebase is also mostly based on this style. I disabled it for now, will enable if this is a useful forrubocop
s own codebase.While implementing this cop (especially autocorrection), I noticed that it would be great to have the following cops: one that checks for empty
class << self
and another that checks for multipleclass << self
definitions and combines them. The second one will greatly simplify (not yet implemented) autocorrection forself_class
style. Will implement both of them if sounds good.