diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2021-12-26 22:39:48 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2021-12-26 23:28:54 +0900 |
commit | c956f979e5d05900315d2753d5c3b1389af8dae4 (patch) | |
tree | 9df416d33a27d671d9f7c9112d0cf785b7810c07 /struct.c | |
parent | abad017354a5e6a03582a8dde58c66caec2bc924 (diff) |
Initialize Struct by calling with keyword arguments
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5348
Diffstat (limited to 'struct.c')
-rw-r--r-- | struct.c | 23 |
1 files changed, 16 insertions, 7 deletions
@@ -685,12 +685,25 @@ rb_struct_initialize_m(int argc, const VALUE *argv, VALUE self) return Qnil; } - VALUE keyword_init = rb_struct_s_keyword_init(klass); - if (RTEST(keyword_init)) { - struct struct_hash_set_arg arg; + bool keyword_init = false; + switch (rb_struct_s_keyword_init(klass)) { + default: if (argc > 1 || !RB_TYPE_P(argv[0], T_HASH)) { rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 0)", argc); } + keyword_init = true; + break; + case Qfalse: + break; + case Qnil: + if (argc > 1 || !RB_TYPE_P(argv[0], T_HASH)) { + break; + } + keyword_init = rb_keyword_given_p(); + break; + } + if (keyword_init) { + struct struct_hash_set_arg arg; rb_mem_clear((VALUE *)RSTRUCT_CONST_PTR(self), n); arg.self = self; arg.unknown_keywords = Qnil; @@ -704,10 +717,6 @@ rb_struct_initialize_m(int argc, const VALUE *argv, VALUE self) if (n < argc) { rb_raise(rb_eArgError, "struct size differs"); } - if (NIL_P(keyword_init) && argc == 1 && RB_TYPE_P(argv[0], T_HASH) && rb_keyword_given_p()) { - rb_warn("Passing only keyword arguments to Struct#initialize will behave differently from Ruby 3.2. "\ - "Please use a Hash literal like .new({k: v}) instead of .new(k: v)."); - } for (long i=0; i<argc; i++) { RSTRUCT_SET(self, i, argv[i]); } |