Farrel Lifson
9/26/2007 7:02:00 AM
On 26/09/2007, Jan Ask <janaskhoej@gmail.com> wrote:
> Hi all,
>
> I have looked at some of the answers in the forum, but they do not seem
> to fit. I want to merge two array into a hash like so:
>
> key = [ "1", "2", "3"]
> value = [ "a", "b" ]
>
> into:
>
> myhash = {'1' => 'a' , '2' => 'b' , '3' => ''}
>
> Can this be done without creating new classes and methods?
irb(main):001:0> key = ["1","2","3"]
=> ["1", "2", "3"]
irb(main):002:0> value = ["a","b","c"]
=> ["a", "b"]
irb(main):009:0> Hash[*key.zip(value).flatten]
=> {"1"=>"a", "2"=>"b", "3"=>nil}