From d4da74ea786da7906fdb85e593593a9c6c11fe96 Mon Sep 17 00:00:00 2001 From: Kazuki Tsujimoto Date: Fri, 8 Nov 2019 11:37:07 +0900 Subject: [PATCH] Define Struct#deconstruct_keys --- struct.c | 28 ++++++++++++++++++++++++++++ test/ruby/test_pattern_matching.rb | 24 ++++++++++++++++++++++++ test/ruby/test_struct.rb | 12 ++++++++++++ 3 files changed, 64 insertions(+) diff --git a/struct.c b/struct.c index 273c719aec..f4ae094f00 100644 --- a/struct.c +++ b/struct.c @@ -944,6 +944,33 @@ rb_struct_to_h(VALUE s) return h; } +static VALUE +rb_struct_deconstruct_keys(VALUE s, VALUE keys) +{ + VALUE h; + long i; + + if (NIL_P(keys)) { + return rb_struct_to_h(s); + } + if (UNLIKELY(!RB_TYPE_P(keys, T_ARRAY))) { + rb_raise(rb_eTypeError, + "wrong argument type %"PRIsVALUE" (expected Array or nil)", + rb_obj_class(keys)); + + } + h = rb_hash_new_with_size(RARRAY_LEN(keys)); + for (i=0; i