mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
Classes made from Struct should have default new
singleton method.
[Bug #16465] [Bug #16801] [Fix GH-2795] [Fix GH-2944] [Fix GH-3045] [Fix GH-3093] Note: Backporting shouldn't modify object.h and instead can use struct_new_kw which is basically a duplicate implementation of rb_class_new_instance_pass_kw Co-authored-by: Yusuke Endoh <mame@ruby-lang.org> Co-authored-by: John Hawthorn <john@hawthorn.email> Co-authored-by: Adam Hess <HParker@github.com> Co-authored-by: Jose Cortinas <jacortinas@gmail.com> Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
This commit is contained in:
parent
da345adc1c
commit
adf709a785
Notes:
git
2020-05-08 17:19:12 +09:00
4 changed files with 24 additions and 22 deletions
21
struct.c
21
struct.c
|
@ -317,24 +317,15 @@ rb_struct_s_inspect(VALUE klass)
|
|||
}
|
||||
|
||||
static VALUE
|
||||
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)
|
||||
setup_struct(VALUE nstr, VALUE members)
|
||||
{
|
||||
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", new_func, -1);
|
||||
rb_define_singleton_method(nstr, "[]", new_func, -1);
|
||||
rb_define_singleton_method(nstr, "new", 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, "inspect", rb_struct_s_inspect, 0);
|
||||
len = RARRAY_LEN(members);
|
||||
|
@ -449,7 +440,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, 0);
|
||||
return setup_struct(st, ary);
|
||||
}
|
||||
|
||||
VALUE
|
||||
|
@ -462,7 +453,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, 0);
|
||||
return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -581,7 +572,7 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
|
|||
else {
|
||||
st = new_struct(name, klass);
|
||||
}
|
||||
setup_struct(st, rest, (int)keyword_init);
|
||||
setup_struct(st, rest);
|
||||
rb_ivar_set(st, id_keyword_init, keyword_init);
|
||||
if (rb_block_given_p()) {
|
||||
rb_mod_module_eval(0, 0, st);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue