From: yermej Date: 2008-01-10T09:04:57+09:00 Subject: Re: About array to hash conversion On Jan 9, 3:31 pm, Holden Holden wrote: > Hi, > > I want to convert an array (with pairs [key,value]) to a hash. After > googling a bit I got: > > 1) Hash[*array.flatten] > > But this is not safe as Array#flatten is recursive. > > 2) array.inject({}) { |m, e| m[e[0]] = e[1]; m } > > Too ugly to consider. > > 3) Hash[*array.flatten(1)] > > That's my own attempt. It only works for Ruby > 1.9. > > But I am still wondering why... > > - Array objects have no "to_h" method? > - Hasy[*array] don't accept the [key,value] pairs (as Hash#to_a calls > return)? These would only work in one specific case. What would this do? [1, 2, 3].to_h