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

前端 未结 4 1494
花落未央
花落未央 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:06

    How about this one?

    [a, b].transpose.to_h
    

    If you use Ruby 1.9:

    Hash[ [a, b].transpose ]
    

    I feel a.zip(b) looks like a is master and b is slave, but in this style they are flat.

提交回复
热议问题