Converting Ruby array into a hash

后端 未结 6 1987
甜味超标
甜味超标 2021-01-29 15:54

I am attempting to write a method named my_transform that takes an array as follows:

items = [\"Aqua\", \"Blue\", \"Green\", \"Red\", \"Yellow\"]
         


        
6条回答
  •  不要未来只要你来
    2021-01-29 16:30

    items = ["Aqua", "Blue", "Green", "Red", "Yellow"]
    def my_transform(items)     
       Hash[items.each_with_index.map { |value, index| [value, index] }]
    end
    

提交回复
热议问题