1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

struct.c: Add keyword_init? singleton method for StructClass (#4609)

Fixes [Feature #18008]
This commit is contained in:
hkdnet 2021-07-15 18:14:27 +09:00 committed by GitHub
parent 3ce2bf4d90
commit 1a63754416
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2021-07-15 18:14:51 +09:00
Merged-By: nurse <naruse@airemix.jp>

View file

@ -347,6 +347,29 @@ rb_struct_s_inspect(VALUE klass)
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)
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);