Project

General

Profile

Actions

Feature #15143

closed

Extend `Enumerable#to_h`

Added by sawa (Tsuyoshi Sawada) over 5 years ago. Updated over 5 years ago.

Status:
Closed
Target version:
-
[ruby-core:89088]

Description

Often, we call Array#to_h to the result of Enumerable#map:

(1..5).map{|x| [x, x ** 2]}.to_h
#=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}

I am thinking of a feature to do this in a single method call.

Currently, Enumerable#to_h does not accept a block. I propose that, when Enumerable#to_h is called with a block (that has a subarray representing a key-value pair), return a hash that would be returned by applying the block to map, and to_h to the result:

(1..5).to_h{|x| [x, x ** 2]}
#=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}

Ideally, I request this to be done internally to Ruby without creating an intermediate parent array.


Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Feature #10208: Passing block to Enumerable#to_hClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0