Ruby convert array to nested hash

后端 未结 1 680
天涯浪人
天涯浪人 2020-12-12 20:11

I have the following:

value = 42  
array = [\"this\",\"is\",\"a\",\"test\"]

how can I convert that to get this

{ \"this\" =         


        
相关标签:
1条回答
  • 2020-12-12 21:07

    Try this:

    h = array.reverse.inject(value) { |a, n| { n => a } }
    
    0 讨论(0)
提交回复
热议问题