From 31ac8efca8ecb574e1e7b7c32cce54cb1b97f19a Mon Sep 17 00:00:00 2001 From: Jean byroot Boussier Date: Tue, 23 May 2023 15:51:28 +0200 Subject: Hash.new: print a deprecation warning when receiving keyword arguments (#7828) [Feature #19236] In Ruby 3.3, `Hash.new` shall print a deprecation warning if keyword arguments are passed instead of treating them as an implicit positional Hash. This will allow to safely introduce a `capacity` keyword argument in 3.4 Co-authored-by: Jean Boussier --- hash.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'hash.c') diff --git a/hash.c b/hash.c index 63a51f54c4..323e0ad512 100644 --- a/hash.c +++ b/hash.c @@ -1714,17 +1714,21 @@ set_proc_default(VALUE hash, VALUE proc) static VALUE rb_hash_initialize(int argc, VALUE *argv, VALUE hash) { - VALUE ifnone; - rb_hash_modify(hash); + if (rb_block_given_p()) { rb_check_arity(argc, 0, 0); - ifnone = rb_block_proc(); - SET_PROC_DEFAULT(hash, ifnone); + SET_PROC_DEFAULT(hash, rb_block_proc()); } else { rb_check_arity(argc, 0, 1); - ifnone = argc == 0 ? Qnil : argv[0]; + + VALUE options, ifnone; + rb_scan_args(argc, argv, "01:", &ifnone, &options); + if (NIL_P(ifnone) && !NIL_P(options)) { + ifnone = options; + rb_warn_deprecated_to_remove("3.4", "Calling Hash.new with keyword arguments", "Hash.new({ key: value })"); + } RHASH_SET_IFNONE(hash, ifnone); } -- cgit v1.2.3