Project

General

Profile

Feature #12534

Refinements: refine modules as well

Added by chucke (Tiago Cardoso) over 3 years ago. Updated about 3 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:76199]

Description

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:

module Extension
refine Array do
def self.foo ; puts "bar" ; end
end
end

This code breaks:

module Extension
refine Kernel do
def foo ; puts "bar" ; end
end
end

Associated revisions

Revision a463ab1f
Added by shugo (Shugo Maeda) about 3 years ago

  • eval.c (rb_mod_refine): refine modules as well. [ruby-core:76199] [Feature #12534]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56213 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 56213
Added by shugo (Shugo Maeda) about 3 years ago

  • eval.c (rb_mod_refine): refine modules as well. [ruby-core:76199] [Feature #12534]

Revision 56213
Added by shugo (Shugo Maeda) about 3 years ago

  • eval.c (rb_mod_refine): refine modules as well. [ruby-core:76199] [Feature #12534]

Revision 56213
Added by shugo (Shugo Maeda) about 3 years ago

  • eval.c (rb_mod_refine): refine modules as well. [ruby-core:76199] [Feature #12534]

Revision 56213
Added by shugo (Shugo Maeda) about 3 years ago

  • eval.c (rb_mod_refine): refine modules as well. [ruby-core:76199] [Feature #12534]

Revision a23878c3
Added by nobu (Nobuyoshi Nakada) about 3 years ago

vm_method.c: update assertion [ci skip]

  • vm_method.c (prepare_callable_method_entry): update assertion as defined_class may be T_MODULE not only I_ICLASS since r56213. [Feature #12534]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56380 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 56380
Added by nobu (Nobuyoshi Nakada) about 3 years ago

vm_method.c: update assertion [ci skip]

  • vm_method.c (prepare_callable_method_entry): update assertion as defined_class may be T_MODULE not only I_ICLASS since r56213. [Feature #12534]

Revision 56380
Added by nobu (Nobuyoshi Nakada) about 3 years ago

vm_method.c: update assertion [ci skip]

  • vm_method.c (prepare_callable_method_entry): update assertion as defined_class may be T_MODULE not only I_ICLASS since r56213. [Feature #12534]

Revision 56380
Added by nobu (Nobuyoshi Nakada) about 3 years ago

vm_method.c: update assertion [ci skip]

  • vm_method.c (prepare_callable_method_entry): update assertion as defined_class may be T_MODULE not only I_ICLASS since r56213. [Feature #12534]

Revision 56380
Added by nobu (Nobuyoshi Nakada) about 3 years ago

vm_method.c: update assertion [ci skip]

  • vm_method.c (prepare_callable_method_entry): update assertion as defined_class may be T_MODULE not only I_ICLASS since r56213. [Feature #12534]

Revision 41f7aa64
Added by nobu (Nobuyoshi Nakada) about 3 years ago

vm_insnhelper.c: update assertion [ci skip]

  • vm_insnhelper.c (callable_class_p): update assertion as callable class may be T_MODULE or I_ICLASS which refines a module since r56213. [Feature #12534]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56381 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 56381
Added by nobu (Nobuyoshi Nakada) about 3 years ago

vm_insnhelper.c: update assertion [ci skip]

  • vm_insnhelper.c (callable_class_p): update assertion as callable class may be T_MODULE or I_ICLASS which refines a module since r56213. [Feature #12534]

Revision 56381
Added by nobu (Nobuyoshi Nakada) about 3 years ago

vm_insnhelper.c: update assertion [ci skip]

  • vm_insnhelper.c (callable_class_p): update assertion as callable class may be T_MODULE or I_ICLASS which refines a module since r56213. [Feature #12534]

Revision 56381
Added by nobu (Nobuyoshi Nakada) about 3 years ago

vm_insnhelper.c: update assertion [ci skip]

  • vm_insnhelper.c (callable_class_p): update assertion as callable class may be T_MODULE or I_ICLASS which refines a module since r56213. [Feature #12534]

Revision 56381
Added by nobu (Nobuyoshi Nakada) about 3 years ago

vm_insnhelper.c: update assertion [ci skip]

  • vm_insnhelper.c (callable_class_p): update assertion as callable class may be T_MODULE or I_ICLASS which refines a module since r56213. [Feature #12534]

History

Updated by shevegen (Robert A. Heiler) over 3 years ago

Interesting. I was not aware of this.

It is however mentioned in the documentation:

http://ruby-doc.org/core-2.3.1/doc/syntax/refinements_rdoc.html

Quote:

"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) over 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.

Updated by matz (Yukihiro Matsumoto) about 3 years ago

I understand the implementation difficulty. If we prohibit super in refined methods in Modules, is it possible to refine modules?
If it's reasonably possible, I'd like to accept this.

Matz.

#4

Updated by shugo (Shugo Maeda) about 3 years ago

  • Status changed from Feedback to Closed

Applied in changeset r56213.


  • eval.c (rb_mod_refine): refine modules as well. [ruby-core:76199] [Feature #12534]

Also available in: Atom PDF