我有一个字符串数组,想把它转换成一个散列,其中
array[0]
是键,array[1]
是值,然后array[2]
是下一组的键。
我曾尝试#each
,#map
,#each_with_object
,#to_h
在组合中的所有方式和最接近我可以是每个阵列元素设置为与零值的键。
# animal_data1 ={}
# animal_data1 = Hash[collected.map {|key,value| [key.to_sym, value]}]
# puts animal_data1
=> {
:"Kingdom:Five groups that classify all living things"=>nil,
:Animalia=>nil,
:"Phylum:A group of animals within the animal kingdom"=>nil,
:Chordata=>nil,
:"Class:A group of animals within a pylum"=>nil,
:Mammalia=>nil,
:"Order:A group of animals within a class"=>nil,
:Tubulidentata=>nil,
:"Family:A group of animals within an order"=>nil
}
Run Code Online (Sandbox Code Playgroud)
arr = [:a, :b, :c, :d]
Run Code Online (Sandbox Code Playgroud)
Hash[*arr]
#=> {:a=>:b, :c=>:d}
Run Code Online (Sandbox Code Playgroud)
见哈希::[]。
Hash[*arr]
在这里与:
Hash[:a, :b, :c, :d]
Run Code Online (Sandbox Code Playgroud)