Skip to content

Commit

Permalink
Use rb_struct_initialize to initialize Data
Browse files Browse the repository at this point in the history
  • Loading branch information
nevans committed Nov 26, 2024
1 parent ed501ed commit 2292f0b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ext/psych/psych_to_ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ static VALUE path2class(VALUE self, VALUE path)
return rb_path_to_class(path);
}

static VALUE init_struct(VALUE self, VALUE data, VALUE attrs)
{
VALUE args = rb_ary_new2(1);
rb_ary_push(args, attrs);
rb_struct_initialize(data, args);

return data;
}

void Init_psych_to_ruby(void)
{
VALUE psych = rb_define_module("Psych");
Expand All @@ -33,6 +42,7 @@ void Init_psych_to_ruby(void)
VALUE visitor = rb_define_class_under(visitors, "Visitor", rb_cObject);
cPsychVisitorsToRuby = rb_define_class_under(visitors, "ToRuby", visitor);

rb_define_private_method(cPsychVisitorsToRuby, "init_struct", init_struct, 2);
rb_define_private_method(cPsychVisitorsToRuby, "build_exception", build_exception, 2);
rb_define_private_method(class_loader, "path2class", path2class, 1);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/psych/visitors/to_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ def visit_Psych_Nodes_Mapping o
revive_data_members(members, o)
end
data ||= allocate_anon_data(o, members)
data.send(:initialize, **members)
init_struct(data, **members)
data.freeze
data

when /^!ruby\/object:?(.*)?$/
Expand Down

0 comments on commit 2292f0b

Please sign in to comment.