From: Markus Date: 2004-09-09T13:28:41+09:00 Subject: Re: [RCR] Unified type conversion framework On Wed, 2004-09-08 at 21:17, Gavin Sinclair wrote: > > Florian Gross wrote: > >> Paul Brannan wrote: > >> > >>> I think you mean Hash[*anArray]. IMO, It's not totally unintuitive, > >>> but it's not consistent with other conversions. > >> > >> > >> Actually converting an assoc array to a Hash is done via > >> Hash[*array.flatten]. I think that that is pretty low level. I'd > >> prefer something like Hash.from_assoc() > > > > At work we have Array#to_h in a library, it's really useful for things > > like: > > > > db.select_all(...).map {|foo,bar| [foo,bar]}.to_h > > > > Sam > > This is my approach (very similar) and I love it: > > require 'extensions/all' > db.select_all(...).build_hash { |foo, bar| [foo,bar] } > > Gavin > Well, as long as we're playing "show me yours and I'll show you mine," I use (again, very similar): module Enumerable def collect_hash result = {} each { |x| result[x] = yield(x) } result end end and thus would write (for the initial): db.select_all(...).collect_hash { |foo, bar| bar } -- Markus