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

ruby-changes:66794

From: hkdnet <ko1@a...>
Date: Thu, 15 Jul 2021 18:14:42 +0900 (JST)
Subject: [ruby-changes:66794] 1a63754416 (master): struct.c: Add keyword_init? singleton method for StructClass (#4609)

https://git.ruby-lang.org/ruby.git/commit/?id=1a63754416

From 1a637544166eca6b917fb6f32baeb771f4914b7a Mon Sep 17 00:00:00 2001
From: hkdnet <satoko.itse@g...>
Date: Thu, 15 Jul 2021 18:14:27 +0900
Subject: struct.c: Add keyword_init? singleton method for StructClass (#4609)

Fixes [Feature #18008]
---
 struct.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/struct.c b/struct.c
index ceb025f..09cce01 100644
--- a/struct.c
+++ b/struct.c
@@ -347,6 +347,29 @@ rb_struct_s_inspect(VALUE klass) https://github.com/ruby/ruby/blob/trunk/struct.c#L347
     return inspect;
 }
 
+/*
+ * call-seq:
+ *   StructClass.keyword_init? -> true or false
+ *
+ * Returns true if the class was initialized with +keyword_init: true+.
+ * Otherwise returns false.
+ *
+ * Examples:
+ *   Foo = Struct.new(:a)
+ *   Foo.keyword_init? # => false
+ *   Bar = Struct.new(:a, keyword_init: true)
+ *   Bar.keyword_init? # => true
+ */
+static VALUE
+rb_struct_s_keyword_init_p(VALUE klass)
+{
+    if (RTEST(rb_struct_s_keyword_init(klass))) {
+        return Qtrue;
+    } else {
+        return Qfalse;
+    }
+}
+
 static VALUE
 setup_struct(VALUE nstr, VALUE members)
 {
@@ -359,6 +382,8 @@ setup_struct(VALUE nstr, VALUE members) https://github.com/ruby/ruby/blob/trunk/struct.c#L382
     rb_define_singleton_method(nstr, "[]", rb_class_new_instance_pass_kw, -1);
     rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0);
     rb_define_singleton_method(nstr, "inspect", rb_struct_s_inspect, 0);
+    rb_define_singleton_method(nstr, "keyword_init?", rb_struct_s_keyword_init_p, 0);
+
     len = RARRAY_LEN(members);
     for (i=0; i< len; i++) {
         VALUE sym = RARRAY_AREF(members, i);
-- 
cgit v1.1


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

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