From: sawadatsuyoshi@... Date: 2018-09-21T02:23:47+00:00 Subject: [ruby-core:89114] [Ruby trunk Feature#15143] Extend `Enumerable#to_h` Issue #15143 has been updated by sawa (Tsuyoshi Sawada). nobu (Nobuyoshi Nakada) wrote: > OK, not only `Enumerable`, but `Array`, `Hash`, `ENV`, `NilClass` and `Struct` too? Thanks for reminding me about that. Looks like nobu has already implemented the feature for Array and Hash, which are ancestors of `Enumerable`. I also became aware that `Struct` instances constructed by `Struct` are ancestors of `Enumerable`, so it makes sense to implement it for that as well. And, if possible, implementing it also for `ENV` and `NilClass`, which are not ancestors of `Enumerable`, might not be crucial as well, but will may make it convenient nonetheless. ---------------------------------------- Feature #15143: Extend `Enumerable#to_h` https://bugs.ruby-lang.org/issues/15143#change-74132 * Author: sawa (Tsuyoshi Sawada) * Status: Closed * Priority: Normal * Assignee: nobu (Nobuyoshi Nakada) * Target version: ---------------------------------------- Often, we call `Array#to_h` to the result of `Enumerable#map`: ```ruby (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: ```ruby (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. -- https://bugs.ruby-lang.org/ Unsubscribe: