Ruby 创建多维散列数组

Ruby 创建多维散列数组,ruby,loops,hash,multidimensional-array,Ruby,Loops,Hash,Multidimensional Array,我想在多维数组中循环: array = [[1,2,3,4,5], [6,7,8,9,10]] 并使用另一个数组中的键创建哈希: keyValues = "one","two","three","four","five" 我有以下代码来执行此操作: hash = Hash.new multiArray = Array.new array.each do |values| keyValues.each do |key| i = keyValues.index(key) h

我想在多维数组中循环:

array = [[1,2,3,4,5], [6,7,8,9,10]] 
并使用另一个数组中的键创建哈希:

keyValues = "one","two","three","four","five"
我有以下代码来执行此操作:

hash = Hash.new
multiArray = Array.new
array.each do |values|
  keyValues.each do |key|
    i = keyValues.index(key)
    hash[key] = values[i]
  end
  puts hash
  multiArray << hash     
end
puts multiArray
最后的
多数组
是:

{"one"=>6, "two"=>7, "three"=>8, "four"=>9, "five"=>10}
{"one"=>6, "two"=>7, "three"=>8, "four"=>9, "five"=>10}
我不明白为什么我得不到:

{"one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5}

对于最后一个
多数组

,您的数组有两个相同哈希对象的条目。因此,如果在任意位置更改哈希对象,它将在两个数组项中都更改。为了避免每个数组条目中都有相同的哈希对象,可以在插入之前复制哈希,方法是更改
multiArray您的数组有两个相同哈希对象的条目。因此,如果在任意位置更改哈希对象,它将在两个数组项中都更改。为了避免在每个数组条目中都有相同的哈希对象,您可以在插入之前复制哈希,方法是更改
multiArray,既然@August已经发现了代码中的问题,我想建议一种简洁、类似Ruby的方法来获得您想要的结果

代码

def make_hash(array, key_values)
    array.map { |a| key_values.zip(a).to_h }
end
array = [[1,2,3,4,5], [6,7,8,9,10]] 
key_values = ["one","two","three","four","five"]

make_hash(array, key_values)
  #=> [{"one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5},
  #    {"one"=>6, "two"=>7, "three"=>8, "four"=>9, "five"=>10}]
示例

def make_hash(array, key_values)
    array.map { |a| key_values.zip(a).to_h }
end
array = [[1,2,3,4,5], [6,7,8,9,10]] 
key_values = ["one","two","three","four","five"]

make_hash(array, key_values)
  #=> [{"one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5},
  #    {"one"=>6, "two"=>7, "three"=>8, "four"=>9, "five"=>10}]
解释

传递到块中的第一个值为:

所以我们有

b = key_values.zip(a)
  #=> [["one", 1], ["two", 2], ["three", 3], ["four", 4], ["five", 5]]
b.to_h
  #=> {"one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5}
对于Ruby版本<2.0(引入时),我们必须编写
Hash(b)
,而不是
b

类似地,传递给块的第二个值为:

a = [6,7,8,9,10]
所以


既然@August已经发现了您代码中的问题,我想建议一种简洁、类似Ruby的方法来获得您想要的结果

代码

def make_hash(array, key_values)
    array.map { |a| key_values.zip(a).to_h }
end
array = [[1,2,3,4,5], [6,7,8,9,10]] 
key_values = ["one","two","three","four","five"]

make_hash(array, key_values)
  #=> [{"one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5},
  #    {"one"=>6, "two"=>7, "three"=>8, "four"=>9, "five"=>10}]
示例

def make_hash(array, key_values)
    array.map { |a| key_values.zip(a).to_h }
end
array = [[1,2,3,4,5], [6,7,8,9,10]] 
key_values = ["one","two","three","four","five"]

make_hash(array, key_values)
  #=> [{"one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5},
  #    {"one"=>6, "two"=>7, "three"=>8, "four"=>9, "five"=>10}]
解释

传递到块中的第一个值为:

所以我们有

b = key_values.zip(a)
  #=> [["one", 1], ["two", 2], ["three", 3], ["four", 4], ["five", 5]]
b.to_h
  #=> {"one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5}
对于Ruby版本<2.0(引入时),我们必须编写
Hash(b)
,而不是
b

类似地,传递给块的第二个值为:

a = [6,7,8,9,10]
所以


首先,安装
y\u支持
gem(
gem安装y\u支持
)。它定义了用于构造哈希的
Array#>
运算符:

require 'y_support/core_ext/array'
[ :a, :b, :c ] >> [ 1, 2, 3 ]
#=> {:a=>1, :b=>2, :c=>3}
有了它,您的工作可以像这样完成:

array = [1,2,3,4,5], [6,7,8,9,10] 
key_values = ["one","two","three","four","five"]

multi_array = array.map { |a| key_values >> a }
#=> [{"one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5},
     {"one"=>6, "two"=>7, "three"=>8, "four"=>9, "five"=>10}]

首先,安装
y\u支持
gem(
gem安装y\u支持
)。它定义了用于构造哈希的
Array#>
运算符:

require 'y_support/core_ext/array'
[ :a, :b, :c ] >> [ 1, 2, 3 ]
#=> {:a=>1, :b=>2, :c=>3}
有了它,您的工作可以像这样完成:

array = [1,2,3,4,5], [6,7,8,9,10] 
key_values = ["one","two","three","four","five"]

multi_array = array.map { |a| key_values >> a }
#=> [{"one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5},
     {"one"=>6, "two"=>7, "three"=>8, "four"=>9, "five"=>10}]