How to build a Ruby hash out of two equally-sized arrays?

前端 未结 4 1489
花落未央
花落未央 2020-12-22 20:40

I have two arrays

a = [:foo, :bar, :baz, :bof]

and

b = [\"hello\", \"world\", 1, 2]

I want



        
4条回答
  •  失恋的感觉
    2020-12-22 21:22

    h = Hash[a.zip b] # => {:baz=>1, :bof=>2, :bar=>"world", :foo=>"hello"}
    

    ...damn, I love Ruby.

提交回复
热议问题