[前][次][番号順一覧][スレッド一覧]

ruby-changes:58335

From: Nobuyoshi <ko1@a...>
Date: Mon, 21 Oct 2019 15:30:22 +0900 (JST)
Subject: [ruby-changes:58335] 431132f037 (master): Pass the called keyword arguments if `keyword_init`

https://git.ruby-lang.org/ruby.git/commit/?id=431132f037

From 431132f037aecc8c9bc783fea257db653c4f8cb0 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Mon, 21 Oct 2019 12:20:19 +0900
Subject: Pass the called keyword arguments if `keyword_init`


diff --git a/struct.c b/struct.c
index b293574..8294e9f 100644
--- a/struct.c
+++ b/struct.c
@@ -311,15 +311,24 @@ rb_struct_s_inspect(VALUE klass) https://github.com/ruby/ruby/blob/trunk/struct.c#L311
 }
 
 static VALUE
-setup_struct(VALUE nstr, VALUE members)
+struct_new_kw(int argc, const VALUE *argv, VALUE klass)
+{
+    return rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS);
+}
+
+static VALUE
+setup_struct(VALUE nstr, VALUE members, int keyword_init)
 {
     long i, len;
+    VALUE (*new_func)(int, const VALUE *, VALUE) = rb_class_new_instance;
+
+    if (keyword_init) new_func = struct_new_kw;
 
     members = struct_set_members(nstr, members);
 
     rb_define_alloc_func(nstr, struct_alloc);
-    rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1);
-    rb_define_singleton_method(nstr, "[]", rb_class_new_instance, -1);
+    rb_define_singleton_method(nstr, "new", new_func, -1);
+    rb_define_singleton_method(nstr, "[]", new_func, -1);
     rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0);
     rb_define_singleton_method(nstr, "inspect", rb_struct_s_inspect, 0);
     len = RARRAY_LEN(members);
@@ -434,7 +443,7 @@ rb_struct_define(const char *name, ...) https://github.com/ruby/ruby/blob/trunk/struct.c#L443
 
     if (!name) st = anonymous_struct(rb_cStruct);
     else st = new_struct(rb_str_new2(name), rb_cStruct);
-    return setup_struct(st, ary);
+    return setup_struct(st, ary, 0);
 }
 
 VALUE
@@ -447,7 +456,7 @@ rb_struct_define_under(VALUE outer, const char *name, ...) https://github.com/ruby/ruby/blob/trunk/struct.c#L456
     ary = struct_make_members_list(ar);
     va_end(ar);
 
-    return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary);
+    return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary, 0);
 }
 
 /*
@@ -566,7 +575,7 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass) https://github.com/ruby/ruby/blob/trunk/struct.c#L575
     else {
 	st = new_struct(name, klass);
     }
-    setup_struct(st, rest);
+    setup_struct(st, rest, (int)keyword_init);
     rb_ivar_set(st, id_keyword_init, keyword_init);
     if (rb_block_given_p()) {
         rb_mod_module_eval(0, 0, st);
diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb
index 6ba2815..ec7728b 100644
--- a/test/ruby/test_struct.rb
+++ b/test/ruby/test_struct.rb
@@ -112,6 +112,10 @@ module TestStruct https://github.com/ruby/ruby/blob/trunk/test/ruby/test_struct.rb#L112
     assert_equal @Struct::KeywordInitTrue.new(a: 1, b: 2).values, @Struct::KeywordInitFalse.new(1, 2).values
     assert_equal "#{@Struct}::KeywordInitFalse", @Struct::KeywordInitFalse.inspect
     assert_equal "#{@Struct}::KeywordInitTrue(keyword_init: true)", @Struct::KeywordInitTrue.inspect
+    k = Class.new(@Struct::KeywordInitFalse) {def initialize(**) end}
+    assert_warn(/The last argument is used as the keyword parameter/) {k.new(a: 1, b: 2)}
+    k = Class.new(@Struct::KeywordInitTrue) {def initialize(**) end}
+    assert_warn('') {k.new(a: 1, b: 2)}
 
     @Struct.instance_eval do
       remove_const(:KeywordInitTrue)
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]