mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Pass the called keyword arguments if keyword_init
This commit is contained in:
parent
215e4b8721
commit
431132f037
Notes:
git
2019-10-21 15:30:27 +09:00
2 changed files with 19 additions and 6 deletions
21
struct.c
21
struct.c
|
@ -311,15 +311,24 @@ rb_struct_s_inspect(VALUE klass)
|
|||
}
|
||||
|
||||
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, ...)
|
|||
|
||||
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, ...)
|
|||
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)
|
|||
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);
|
||||
|
|
|
@ -112,6 +112,10 @@ module TestStruct
|
|||
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)
|
||||
|
|
Loading…
Add table
Reference in a new issue