From 54198c7b97d3d353f7ac233e0360034b6e7b6cb6 Mon Sep 17 00:00:00 2001 From: Shugo Maeda Date: Wed, 5 Jan 2022 17:47:29 +0900 Subject: Add Module#refinements and Refinement#refined_class [Feature #12737] --- eval.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'eval.c') diff --git a/eval.c b/eval.c index 8103f52fa5..34c5d6ff85 100644 --- a/eval.c +++ b/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 + * + * produces: + * + * [#, #] + */ +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"); -- cgit v1.2.1