Comments (6)
I second this, would actually love to have an optional cop for enforcing the class << self
style as opposed to self.foo
. Summary of benefits:
- indent serves as the visual que
- helps keep all class methods grouped together
- searching
def foo
returns both class and instance methods - adding class-level code requires
class << self
anyway, might as well use it for methods too
Edit: didn't realize coming from google that this was github's styles, not rubocop itself. Still might as well throw in my 2c.
from rubocop-github.
I don't agree with the preference expressed in the guide, but I think I understand the issue. Consider this Ruby code:
class A
class << self
attr_accessor :some_attribute
end
end
class B < A
end
As far as I know, that's the only way to support usage like this:
A.some_attribute = :value_1
B.some_attribute # => nil
B.some_attribute = :value_2
A.some_attribute # => :value_1
I mean, using a class variable would cause it to be shared by A
and B
, which we don't want.
Does that sound right to you? Is there another way to implement that snippet above?
I think the same thing is true for alias
, it only works for class methods inside class << self
.
So, I don't agree with the preference, but I think that explains the difference, does that make sense?
from rubocop-github.
@rmosolgo first of all: thank you for responding! 🎉
That's awesome!
However, I just wrote about
def self.foo
…
def self.bar
…
def self.baz
…
vs
class << self
def foo
…
def bar
…
def baz
…
The case that you alias class methods or use class variables shouldn't occur in every class, right? Especially as class variables are kind of dangerous.
I rather would accept if some class would exceptionally not comply to a style guide than having a less readable variant established as a standard in a style guide.
Even if someone decided that this is the way to write class methods on GitHub, and added it to the style guide because of the points you just mentioned it should probably contain an explanation à la "Hey folks, we write class methods this way because we use class method aliasing and class variables so often".
Besides from that: these aliases don't mention the class methods at all. Are you sure that we are talking about the same issue here?
I really just opened this issue to write about the method definition of the class methods.
from rubocop-github.
I'm sorry. I just recognized that the title of the issue was actually the inverse to what I wrote to in the description 🙈
I'm sorry in case that this was the cause of the misunderstanding here.
By the way, the particular text snipped I referenced here, is this sentence
Avoid
class << self
except when necessary, e.g. single accessors and aliased attributes.
from rubocop-github.
I agree. I think the cop should avoid having both class << self
and def self.blah
in the same file
from rubocop-github.
RuboCop added a cop for this in 2020 that can be configured either way: rubocop/rubocop#8381. So I'd say this one is mostly personal preference, and our style guide now matches the default configuration in RuboCop.
from rubocop-github.
Related Issues (18)
- Improve the purpose/usability of rubocop-github
- Template could not be found when using JBuilder Views
- New Cop: Avoid usage of `Class.descendents`
- Documentation for RailsControllerRender cops
- Add deep links into the styleguide for each cop in `config/default.yml`
- Removed cop causing error HOT 1
- ApplicationRecord subclass cop runs on ApplicationRecord model HOT 2
- Is "avoid using String#+" still valid in the era of frozen_string_literal HOT 2
- Let's beef up this project! HOT 5
- Unify controller render cops?
- namespace changed HOT 2
- Rails cops being removed in RuboCop 0.72 HOT 2
- Update rubocop-performance to 1.5.0 HOT 1
- Metrics/Lin eLength has the wrong namespace - should be Layout HOT 6
- I don't _agree_ with the preference expressed in the guide, but I think I understand the issue. Consider this Ruby code: HOT 1
- Update To Allow Version 1.3.1 Of Rubocop.
- Misleading linting when passing a variable to render `locals:` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rubocop-github.