What is the best way to convert an array to a hash in Ruby

后端 未结 11 1994
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 18:18

In Ruby, given an array in one of the following forms...

[apple, 1, banana, 2]
[[apple, 1], [banana, 2]]

...what is the best way to convert

11条回答
  •  暖寄归人
    2020-11-28 18:53

    Update

    Ruby 2.1.0 is released today. And I comes with Array#to_h (release notes and ruby-doc), which solves the issue of converting an Array to a Hash.

    Ruby docs example:

    [[:foo, :bar], [1, 2]].to_h    # => {:foo => :bar, 1 => 2}
    

提交回复
热议问题