我有一个按特定顺序排列的数组,并希望创建一个散列,数组中的奇数条目作为索引,偶数作为值。这段代码做得很完美,但忽略了数组中的一对值。
resolv_hash = Hash[*namerslv_array]
puts "values in hash"
resolv_hash.each do |key, array|
puts "#{key} " + array
end有人能帮忙吗?
发布于 2016-12-10 00:18:17
我想你想:
resolv_hash = namerslv_array.each_slice(2).to_h说明:
>> array = [1,2,3,4,5,6,7,8,9,0]
>> array.each_slice(2).to_h
=> {1=>2, 3=>4, 5=>6, 7=>8, 9=>0}https://stackoverflow.com/questions/41070772
复制相似问题