Repository navigation
Conversation
`module_function` makes the instance method private and puts a copy on the module itself. The module in this example is a namespace that nothing includes, so it carried a private instance method for every class method the bad example defined. With `def self.`, the good example differs from the bad one only in the `class` keyword.
|
They serve different purposes. module A
module_function
def a
1
end
end
module B # composed interface
extend A
# extend SomeOtherModule
module_function
def b
a + a
end
end
B.b # => 2module A
def self.a
1
end
end
module B
extend A
def self.b
a + a
end
end
B.b # 'B.b': undefined local variable or method 'a' for module B (NameError)More on that in https://practicingruby.com/articles/uses-for-modules-3a Even though the guideline is about not using classes for cases when they are not purposed to be instantiated, I'm reluctant to accepting a change that can result in surprises like above. |
|
This example uses
Math.sqrt(4)
# or
include Math
sqrt(4)
Nothing includes It's true that Could you reopen this? |
|
From the docs:
Please correct me if I’m mistaken that this is when a class includes a module that defines a module function. This is not the case described in the guideline.
Can you please elaborate? What is your perspective of a module function?
I struggle to see this from the doc. Can you please point me to where it is?
What makes you think so? As I briefly mentioned the example in my previous comment shows the usage of such modules that go a bit beyond just using them as standalone module function bags: for building a larger interface out of smaller ones. Think of this: module Math
extend Algebra
extend Trigonometry
extend Logarithms
extend Roots
extend Gamma
endCan you build it with
I agree that this change does affect the section that is focused on something that will be a receiver of class methods only. Not necessarily included or used to extend something. |
|
Answering your questions in order. (1)
|
|
The module in the "Modules vs Classes" example is only ever a receiver. The instance-method half is never reached, so showing Is there a reason to show |
|
This is an argument against the other guideline:
that doesn’t even bother mentioning a totally reasonable I don’t have sufficient historical context to answer why it is so. But my assumption is that there are just too many possible usage scenarios, and with at least those three (and a half with From README:
I clearly see and totally agree with you that what you propose is a better option in certain scenarios. But none of this is sufficient to change the recommended style. Do you want to harvest the real-world-ruby/real-world-rails meta-repos to gather information on usage, purpose and design to show that the change that you propose is the real-world practice? |
|
I haven't argued against the This PR is only about the code example under "Modules vs Classes". My question from my previous comment is still open: is there a reason to show |
|
You mentioned But I think |
|
I clearly see what you mean now. Is my understanding correct? |
|
Yes, that's right. Thank you! And |
|
@pirj Any update? |
module_functionmakes the instance method private and puts a copy on the module itself. The module in this example is a namespace that nothing includes, so it carried a private instance method for every class method the bad example defined.With
def self., the good example differs from the bad one only in theclasskeyword.The
module_functionrule below is unaffected. It applies when a module's methods areinstance methods and you want them callable on the module too, which is what
module_functionis for.Style/StaticClassenforces this rule and its autocorrect had the same problem;rubocop/rubocop#15770 changes it to correct the same way.