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

前端 未结 4 1503
花落未央
花落未央 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 20:55

    Just wanted to point out that there's a slightly cleaner way of doing this:

    h = a.zip(b).to_h # => {:foo=>"hello", :bar=>"world", :baz=>1, :bof=>2}
    

    Have to agree on the "I love Ruby" part though!

提交回复
热议问题