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:
parent
3ce2bf4d90
commit
1a63754416
Notes:
git
2021-07-15 18:14:51 +09:00
Merged-By: nurse <naruse@airemix.jp>
1 changed files with 25 additions and 0 deletions
25
struct.c
25
struct.c
|
@ -347,6 +347,29 @@ rb_struct_s_inspect(VALUE klass)
|
||||||
return inspect;
|
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
|
static VALUE
|
||||||
setup_struct(VALUE nstr, VALUE members)
|
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, "[]", rb_class_new_instance_pass_kw, -1);
|
||||||
rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0);
|
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, "inspect", rb_struct_s_inspect, 0);
|
||||||
|
rb_define_singleton_method(nstr, "keyword_init?", rb_struct_s_keyword_init_p, 0);
|
||||||
|
|
||||||
len = RARRAY_LEN(members);
|
len = RARRAY_LEN(members);
|
||||||
for (i=0; i< len; i++) {
|
for (i=0; i< len; i++) {
|
||||||
VALUE sym = RARRAY_AREF(members, i);
|
VALUE sym = RARRAY_AREF(members, i);
|
||||||
|
|
Loading…
Reference in a new issue