Robert Klemme
5/30/2006 7:12:00 AM
Marcin MielżyÅ?ski wrote:
> Bucco wrote:
>> Is there a simple way of merging two arrays into one has with one array
>> being the keys and the other being the values for the has?
>> a = [1, 2, 3']
>> b = ['a', 'b', 'c']
>> h = {1=>'a', 2=>'b', 3=>'c'}
>
> a = [1, 2, 3]
> b = ['a', 'b', 'c']
> p a.zip(b).inject({}){|h,(k,v)| h[k]=v;h}
Even more efficient:
>> require 'enumerator'
=> true
>> a = [1, 2, 3]
=> [1, 2, 3]
>> b = ['a', 'b', 'c']
=> ["a", "b", "c"]
>> a.to_enum(:zip,b).inject({}) {|h,(k,v)| h[k]=v; h}
=> {1=>"a", 2=>"b", 3=>"c"}
Kind regards
robert