mirror of
https://github.com/ruby/ruby.git
synced 2025-09-15 08:33:58 +02:00
Add Module#refinements and Refinement#refined_class [Feature #12737]
This commit is contained in:
parent
21ee5341f8
commit
54198c7b97
3 changed files with 76 additions and 1 deletions
44
eval.c
44
eval.c
|
@ -1322,7 +1322,12 @@ rb_using_module(const rb_cref_t *cref, VALUE module)
|
|||
rb_clear_method_cache_all();
|
||||
}
|
||||
|
||||
/*! \private */
|
||||
/*
|
||||
* call-seq:
|
||||
* refined_class -> class
|
||||
*
|
||||
* Return the class refined by the receiver.
|
||||
*/
|
||||
VALUE
|
||||
rb_refinement_module_get_refined_class(VALUE module)
|
||||
{
|
||||
|
@ -1457,6 +1462,41 @@ mod_using(VALUE self, VALUE module)
|
|||
return self;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* refinements -> array
|
||||
*
|
||||
* Returns an array of modules defined within the receiver.
|
||||
*
|
||||
* module A
|
||||
* refine Integer do
|
||||
* end
|
||||
*
|
||||
* refine String do
|
||||
* end
|
||||
* end
|
||||
*
|
||||
* p A.refinements
|
||||
*
|
||||
* <em>produces:</em>
|
||||
*
|
||||
* [#<refinement:Integer@A>, #<refinement:String@A>]
|
||||
*/
|
||||
static VALUE
|
||||
mod_refinements(VALUE self)
|
||||
{
|
||||
ID id_refinements;
|
||||
VALUE refinements;
|
||||
|
||||
CONST_ID(id_refinements, "__refinements__");
|
||||
refinements = rb_attr_get(self, id_refinements);
|
||||
if (NIL_P(refinements)) {
|
||||
return rb_ary_new();
|
||||
}
|
||||
return rb_hash_values(refinements);
|
||||
}
|
||||
|
||||
static int
|
||||
used_modules_i(VALUE _, VALUE mod, VALUE ary)
|
||||
{
|
||||
|
@ -1993,12 +2033,14 @@ Init_eval(void)
|
|||
rb_define_private_method(rb_cModule, "prepend_features", rb_mod_prepend_features, 1);
|
||||
rb_define_private_method(rb_cModule, "refine", rb_mod_refine, 1);
|
||||
rb_define_private_method(rb_cModule, "using", mod_using, 1);
|
||||
rb_define_method(rb_cModule, "refinements", mod_refinements, 0);
|
||||
rb_define_singleton_method(rb_cModule, "used_modules",
|
||||
rb_mod_s_used_modules, 0);
|
||||
rb_define_singleton_method(rb_cModule, "used_refinements",
|
||||
rb_mod_s_used_refinements, 0);
|
||||
rb_undef_method(rb_cClass, "refine");
|
||||
rb_define_private_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
|
||||
rb_define_method(rb_cRefinement, "refined_class", rb_refinement_module_get_refined_class, 0);
|
||||
|
||||
rb_undef_method(rb_cClass, "module_function");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue