summaryrefslogtreecommitdiff
path: root/array.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 /array.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 'array.c')
-rw-r--r--array.c14
1 files changed, 12 insertions, 2 deletions
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 <i>ary</i> as an array of
* <tt>[key, value]</tt> 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<RARRAY_LEN(ary); i++) {
- const VALUE elt = rb_ary_elt(ary, i);
+ const VALUE e = rb_ary_elt(ary, i);
+ const VALUE elt = block_given ? rb_yield_force_blockarg(e) : e;
const VALUE key_value_pair = rb_check_array_type(elt);
if (NIL_P(key_value_pair)) {
rb_raise(rb_eTypeError, "wrong element type %"PRIsVALUE" at %ld (expected array)",