From: "marcandre (Marc-Andre Lafortune)" Date: 2012-11-07T12:48:57+09:00 Subject: [ruby-core:49015] [ruby-trunk - Feature #7292] Enumerable#to_h Issue #7292 has been updated by marcandre (Marc-Andre Lafortune). matz (Yukihiro Matsumoto) wrote: > So what's the difference from rejected #7241? As Nathan said, #7241 (and #666) accept a block and are therefore more related to the more complex categorize/associate/... #4151. The implementation for `to_h` would be as simple conceptually as possible. It would be equivalent to: module Enumerable def to_h result = {} each do |key, value| result[key] = value end result end end I believe this is the simplest definition one can think of. It doesn't try to do much, nor is it too strict (in the same way that "two".to_i returns 0). mame (Yusuke Endoh) wrote: > Use the traditional Hash[] in 2.0.0. Indeed, Hash[] can be used instead, except it's really really ugly. I can't think of any other global method we use like this that should be an instance method. It's very natural to transform data into a hashes, but instead of chaining the transformations we have to reverse the flow for this step. E.g. `source.map{...}.to_h.merge(...)` reads naturally, but `Hash[source.map{...}].merge(...)` doesn't. The only other example of SomeClass.[] I can think of is for Set. In that case, it's understandable as Set doesn't have a dedicated creation syntax, so `Set[1, 2, 3]` has its charms. Are there other cases, besides Hash[]? > I'm moving this ticket into the feature tracker. Didn't I create it as a feature request? ---------------------------------------- Feature #7292: Enumerable#to_h https://bugs.ruby-lang.org/issues/7292#change-32535 Author: marcandre (Marc-Andre Lafortune) Status: Assigned Priority: Low Assignee: matz (Yukihiro Matsumoto) Category: core Target version: next minor Now that #to_h is the official method for explicit conversion to Hash, we should also add Enumerable#to_h: Returns a hash for the yielded key-value pairs. [[:name, 'Joe Smith'], [:age, 42]].to_h # => {name: 'Joe Smith', age: 42} With the Ruby tradition of succint documentation I suggest the documentation talk about key-value pairs and there is no need to be explicit about the uninteresting cases like: (1..3).to_h # => {1 => nil, 2 => nil, 3 => nil} [[1, 2], [1, 3]].to_h # => {1 => 3} [[1, 2], []].to_h # => {1 => 2, nil => nil} I see some reactions of people reading about the upcoming 2.0 release like this one: http://globaldev.co.uk/2012/11/ruby-2-0-0-preview-features/#dsq-comment-body-700242476 -- http://bugs.ruby-lang.org/