Method: Module#ruby2_keywords
- Defined in:
- vm_method.c
#ruby2_keywords(method_name, ...) ⇒ nil (private)
For the given method names, marks the method as passing keywords through a normal argument splat. This should only be called on methods that accept an argument splat (*args) but not explicit keywords or a keyword splat. It marks the method such that if the method is called with keyword arguments, the final hash argument is marked with a special flag such that if it is the final element of a normal argument splat to another method call, and that method call does not include explicit keywords or a keyword splat, the final element is interpreted as keywords. In other words, keywords will be passed through the method to other methods.
This should only be used for methods that delegate keywords to another method, and only for backwards compatibility with Ruby versions before 3.0. See www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/ for details on why ruby2_keywords exists and when and how to use it.
This method will probably be removed at some point, as it exists only for backwards compatibility. As it does not exist in Ruby versions before 2.7, check that the module responds to this method before calling it:
module Mod
def foo(meth, *args, &block)
send(:"do_#{meth}", *args, &block)
end
ruby2_keywords(:foo) if respond_to?(:ruby2_keywords, true)
end
However, be aware that if the ruby2_keywords method is removed, the behavior of the foo method using the above approach will change so that the method does not pass through keywords.
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 |
# File 'vm_method.c', line 2569 static VALUE rb_mod_ruby2_keywords(int argc, VALUE *argv, VALUE module) { int i; VALUE origin_class = RCLASS_ORIGIN(module); rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); rb_check_frozen(module); for (i = 0; i < argc; i++) { VALUE v = argv[i]; ID name = rb_check_id(&v); rb_method_entry_t *me; VALUE defined_class; if (!name) { rb_print_undef_str(module, v); } me = search_method(origin_class, name, &defined_class); if (!me && RB_TYPE_P(module, T_MODULE)) { me = search_method(rb_cObject, name, &defined_class); } if (UNDEFINED_METHOD_ENTRY_P(me) || UNDEFINED_REFINED_METHOD_P(me->def)) { rb_print_undef(module, name, METHOD_VISI_UNDEF); } if (module == defined_class || origin_class == defined_class) { switch (me->def->type) { case VM_METHOD_TYPE_ISEQ: if (ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_rest && !ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_kw && !ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.has_kwrest) { ISEQ_BODY(me->def->body.iseq.iseqptr)->param.flags.ruby2_keywords = 1; rb_clear_method_cache(module, name); } else { rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (method accepts keywords or method does not accept argument splat)", QUOTE_ID(name)); } break; case VM_METHOD_TYPE_BMETHOD: { VALUE procval = me->def->body.bmethod.proc; if (vm_block_handler_type(procval) == block_handler_type_proc) { procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval)); } if (vm_block_handler_type(procval) == block_handler_type_iseq) { const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(procval); const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq); if (ISEQ_BODY(iseq)->param.flags.has_rest && !ISEQ_BODY(iseq)->param.flags.has_kw && !ISEQ_BODY(iseq)->param.flags.has_kwrest) { ISEQ_BODY(iseq)->param.flags.ruby2_keywords = 1; rb_clear_method_cache(module, name); } else { rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (method accepts keywords or method does not accept argument splat)", QUOTE_ID(name)); } break; } } /* fallthrough */ default: rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (method not defined in Ruby)", QUOTE_ID(name)); break; } } else { rb_warn("Skipping set of ruby2_keywords flag for %"PRIsVALUE" (can only set in method defining module)", QUOTE_ID(name)); } } return Qnil; } |