Array to Hash Ruby

后端 未结 9 486
无人及你
无人及你 2021-01-29 17:43

Okay so here\'s the deal, I\'ve been googling for ages to find a solution to this and while there are many out there, they don\'t seem to do the job I\'m looking for.

Ba

9条回答
  •  长发绾君心
    2021-01-29 18:19

    Ruby 2.1.0 introduced a to_h method on Array that does what you require if your original array consists of arrays of key-value pairs: http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-to_h.

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

提交回复
热议问题