Skip to content

Commit

Permalink
Add Refinement#target and deprecate Refinement#refined_class
Browse files Browse the repository at this point in the history
[Feature #19714]
  • Loading branch information
shugo committed Jul 31, 2023
1 parent cfd7729 commit a542512
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Expand Up @@ -56,6 +56,11 @@ Note: We're only listing outstanding class updates.
for long running applications. The actual optimizations performed are entirely
implementation specific and may change in the future without notice. [[Feature #18885]

* Refinement

* Add Refinement#target as an alternative of Refinement#refined_class.
Refinement#refined_class is deprecated and will be removed in Ruby 3.4. [[Feature #19714]]

## Stdlib updates

The following default gems are updated.
Expand Down
20 changes: 17 additions & 3 deletions eval.c
Expand Up @@ -1346,9 +1346,9 @@ rb_using_module(const rb_cref_t *cref, VALUE module)

/*
* call-seq:
* refined_class -> class
* target -> class
*
* Return the class refined by the receiver.
* Return the class or module refined by the receiver.
*/
VALUE
rb_refinement_module_get_refined_class(VALUE module)
Expand All @@ -1359,6 +1359,19 @@ rb_refinement_module_get_refined_class(VALUE module)
return rb_attr_get(module, id_refined_class);
}

/*
* call-seq:
* refined_class -> class
*
* Return the class refined by the receiver.
*/
static VALUE
rb_refinement_refined_class(VALUE module)
{
rb_warn_deprecated_to_remove("3.4", "Refinement#refined_class", "Refinement#target");
return rb_refinement_module_get_refined_class(module);
}

static void
add_activated_refinement(VALUE activated_refinements,
VALUE klass, VALUE refinement)
Expand Down Expand Up @@ -2067,7 +2080,8 @@ Init_eval(void)
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_define_method(rb_cRefinement, "target", rb_refinement_module_get_refined_class, 0);
rb_define_method(rb_cRefinement, "refined_class", rb_refinement_refined_class, 0);
rb_undef_method(rb_cRefinement, "append_features");
rb_undef_method(rb_cRefinement, "prepend_features");
rb_undef_method(rb_cRefinement, "extend_object");
Expand Down
4 changes: 3 additions & 1 deletion test/ruby/test_refinement.rb
Expand Up @@ -1798,15 +1798,17 @@ def test_refinements
assert_equal([int_refinement, str_refinement], m.refinements)
end

def test_refined_class
def test_target
refinements = Module.new {
refine Integer do
end

refine String do
end
}.refinements
assert_equal(Integer, refinements[0].target)
assert_equal(Integer, refinements[0].refined_class)
assert_equal(String, refinements[1].target)
assert_equal(String, refinements[1].refined_class)
end

Expand Down

0 comments on commit a542512

Please sign in to comment.