Hi --

On Thu, 2 Mar 2006, James Edward Gray II wrote:

> On Mar 1, 2006, at 2:31 PM, dblack / wobblini.net wrote:
>
>> Hi --
>> 
>> On Wed, 1 Mar 2006, ara.t.howard / noaa.gov wrote:
>> 
>>> it's pretty dang easy to do already:
>>>
>>>   harp:~ > cat a.rb
>>>   animal, sound = %w[dog cat bird], %w[woof meow cheep]
>>>   require 'yaml' and y Hash[*animal.zip(sound).flatten]
>>> 
>>>
>>>   harp:~ > ruby a.rb
>>>   ---
>>>   cat: meow
>>>   bird: cheep
>>>   dog: woof
>> 
>> A good opportunity for my annual plug for the flattenx extension :-)
>> (On RAA, still, I think.)  It lets you flatten by any number of
>> levels, so that you can use that technique even with nested arrays.
>
> It's not too hard to allow nested Arrays in Hash construction even without 
> the library:
>
>>> arr = [[:one, 1], [:two, %w{an Array}], [:three, 2]]
> => [[:one, 1], [:two, ["an", "Array"]], [:three, 2]]
>>> Hash[*arr.inject(Array.new) { |args, a| args.push(*a) }]
> => {:two=>["an", "Array"], :three=>2, :one=>1}

I may be in the minority, but I prefer:

   Hash[*arr.flatten_once]

:-)


David

-- 
David A. Black (dblack / wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black