From abe75149d14d3286d3051c9e961ab6473a243a19 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 20 Sep 2018 15:06:56 +0000 Subject: 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 --- array.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'array.c') diff --git a/array.c b/array.c index 4b62394daf..c58b316525 100644 --- a/array.c +++ b/array.c @@ -2171,13 +2171,20 @@ rb_ary_to_a(VALUE ary) /* * call-seq: - * ary.to_h -> hash + * ary.to_h -> hash + * ary.to_h { block } -> hash * * Returns the result of interpreting ary as an array of * [key, value] pairs. * * [[:foo, :bar], [1, 2]].to_h * # => {:foo => :bar, 1 => 2} + * + * If a block is given, the results of the block on each element of + * the array will be used as pairs. + * + * ["foo", "bar"].to_h {|s| [s.ord, s]} + * # => {102=>"foo", 98=>"bar"} */ static VALUE @@ -2185,8 +2192,11 @@ rb_ary_to_h(VALUE ary) { long i; VALUE hash = rb_hash_new_with_size(RARRAY_LEN(ary)); + int block_given = rb_block_given_p(); + for (i=0; i