From: Nathan Broadbent Date: 2012-10-31T04:23:07+09:00 Subject: [ruby-core:48626] Re: [ruby-trunk - Feature #7241][Open] Enumerable#to_h proposal --14dae93b60242eff9e04cd4bb761 Content-Type: text/plain; charset=ISO-8859-1 > `each_with_object` isn't specific to hashes, and isn't doing list translation like `map` does. I think `each_with_hash` would be really useful. Not only for cases where you would normally use `each_with_object({})`, but it would also be much nicer to write `each_with_hash([])` instead of `each_with_object(Hash.new([]))`. I had another idea that I'd like to mention: Since you'd often be using default hash values with `each_with_hash`, I would find it useful if there was an easy way to convert the default value into a proc that sets the hash value when a key is looked up, instead of writing `-> hash, key { hash[key] = }`. My intention is to be able to write code like: `[1, 2, 1].each_with_hash([], true) {|h, v| h[v] << v ** 2 }`, where the `true` arg would convert the default value into the proc above. (Personal preference, but I don't really like `+= []`). Does anyone else think that could be useful? > IOW, it sounds perfect for ActiveSupport. ;-) Let me know if you think `each_with_hash` is a good idea, and if it should be added to ActiveSupport. I could open a pull request on Rails or start a new Issue for Ruby :) Best, Nathan --14dae93b60242eff9e04cd4bb761 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable > `each_with_object` isn't sp= ecific to hashes, and isn't doing list
translation like `map` does.<= /span>
=A0
I think `each_with_hash` would be really useful. N= ot only for cases where you would normally use `each_with_object({})`, but = it would also be much nicer to write `each_with_hash([])` instead of `each_= with_object(Hash.new([]))`.

I had another idea that I'd like to mention: Since = you'd often be using default hash values with `each_with_hash`, I would= find it useful if there was an easy way to convert the default value into = a proc that sets the hash value when a key is looked up, instead of writing= =A0`-> hash, key { hash[key] =3D <default> }`.
My intention is to be able to write code like: `[1, 2, 1].each_with_hash([]= , true) {|h, v| h[v] << v ** 2 }`, where the `true` arg would convert= the default value into the proc above. (Personal preference, but I don'= ;t really like `+=3D []`).=A0Does an= yone else think that could be useful?


> I= OW, it sounds perfect for ActiveSupport. ;-)

Let me know if you think `each_with_hash` is a good idea, and= if it should be added to ActiveSupport. I could open a pull request on Rai= ls or start a new Issue for Ruby :)


Best,
Nathan
--14dae93b60242eff9e04cd4bb761--