Refinements: refine modules as well
Refinements were added as a feature to scope monkey-patches on ruby core elements. This works for elements such as String, Array (all classes), but not for modules (Enumerable, Timeout...).
This might be related to refinements not working for singleton classes (for example, I can't define a class method inside a refine block).
This code is evaluated, but the method is undefined:
refine Array do
def self.foo ; puts "bar" ; end
This code breaks:
refine Kernel do
def foo ; puts "bar" ; end
Updated by shevegen (Robert A. Heiler) almost 3 years ago
Interesting. I was not aware of this.
It is however mentioned in the documentation:
"Refinements only modify classes, not modules so the argument must be a class."
As to why, I don't know either. Perhaps the documentation can add some words
to briefly state whether this is on purpose or not.
Updated by shugo (Shugo Maeda) almost 3 years ago
- Assignee set to shugo (Shugo Maeda)
- Status changed from Open to Feedback
There is an implementation difficulty when refining modules and calling
super in that refinement.
One solution is prohibiting
super in a refinement for a module.
Other proposals (and patches) are welcome.