summaryrefslogtreecommitdiff
path: root/struct.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-20 15:06:56 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-20 15:06:56 +0000
commitabe75149d14d3286d3051c9e961ab6473a243a19 (patch)
treef33ad9fcd4569f51d7f0b85fefb751597211438d /struct.c
parente76eebd79be9fa3cb59d0ea6e4ce8214cc6e56ad (diff)
Enumerable#to_h with block and so on
[Feature #15143] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64794 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/struct.c b/struct.c
index 54aa510b01..f2db11cb35 100644
--- a/struct.c
+++ b/struct.c
@@ -880,9 +880,14 @@ rb_struct_to_h(VALUE s)
VALUE h = rb_hash_new_with_size(RSTRUCT_LEN(s));
VALUE members = rb_struct_members(s);
long i;
+ int block_given = rb_block_given_p();
for (i=0; i<RSTRUCT_LEN(s); i++) {
- rb_hash_aset(h, rb_ary_entry(members, i), RSTRUCT_GET(s, i));
+ VALUE k = rb_ary_entry(members, i), v = RSTRUCT_GET(s, i);
+ if (block_given)
+ rb_hash_set_pair(h, rb_yield_values(2, k, v));
+ else
+ rb_hash_aset(h, k, v);
}
return h;
}