From: Trans Date: 2005-10-25T15:02:02+09:00 Subject: Re: RCR: Array#to_h BTW have you tried: module Enumerable # Like #map/#collect, but it generates a Hash. The block # is expected to return two values: the key and the value for the new hash. # # numbers = (1..3) # squares = numbers.graph { |n| [n, n*n] } # { 1=>1, 2=>4, 3=>9 } # sq_roots = numbers.graph { |n| [n*n, n] } # { 1=>1, 4=>2, 9=>3 } # #-- # Credit for original version goes to Zallus Kanite and Gavin Sinclair. #++ def graph(&yld) if yld inject({}) do |h,kv| nk, nv = yld[*kv] h[nk] = nv h end else Hash[*self.to_a.flatten] end end end