有没有解决方案绕过'迭代过程中无法将新密钥添加到哈希(RuntimeError)'?

时间:2021-11-06 20:38:48

I have a big problem with the expected RuntimeError: "can't add a new key into hash during iteration"

我对预期的RuntimeError有一个很大的问题:“在迭代期间无法在哈希中添加新密钥”

In my case a I have a YAML file: test.yaml - in which I have some keys already added.

在我的情况下,我有一个YAML文件:test.yaml - 我已经添加了一些键。

test.yaml
key1:
key2:
key3:

I am getting the contents of the file in a variable:

我在变量中获取文件的内容:

file_hash = YAML.load_file("testm.yaml")

Then I need to loop through this hash and add other keys to them:

然后我需要遍历此哈希并向其添加其他键:

file_hash.each do |key|
   file_hash[key] = 'key_1'
   file_hash[key] = 'key_2'
end
File.open('test.yaml', 'w') { |f| YAML.dump(file_hash, f) }

The main issue is that I am unable to write into a hash while in a loop. I don't understand why this is expected, when you have the power to control the loop block. Is there another way in which I can accomplish what I showed above?

主要问题是我在循环中无法写入哈希。当你有权控制循环块时,我不明白为什么会这样。还有另一种方法可以完成我上面展示的内容吗?

Note: I am using RUBY 1.9.3 p547

注意:我使用的是RUBY 1.9.3 p547

3 个解决方案

#1


11  

Ruby since 1.9 is using lazy iteration, thus they forbid to add new keys to the hash you iterating over. As a solution you can easily duplicate hash or convert it to array before doing each.

Ruby从1.9开始使用延迟迭代,因此它们禁止向迭代的哈希添加新键。作为一种解决方案,您可以轻松地复制哈希或将其转换为数组,然后再执

irb(main):001:0> a={1=>1}; a.each {|k,v| a[2] = 2}
RuntimeError: can't add a new key into hash during iteration

irb(main):002:0> a={1=>1}; a.clone.each {|k,v| a[2] = 2}; a
=> {1=>1, 2=>2}

irb(main):003:0> a={1=>1}; a.to_a.each {|k,v| a[2] = 2}; a
=> {1=>1, 2=>2}

#2


9  

You're modifying the hash you're iterating over. You can't do it.

你正在修改你正在迭代的哈希。你不能这样做。

Instead try another approach:

而是尝试另一种方法:

keys = [1,2,3,4]
file_hash = YAML.load_file("testm.yaml")
keys.each{ |key| file_hash[key] = 'key1' }
# => {1 => 'key1', 2 => 'key1', 3 => 'key1', 4 => 'key1'}

#3


0  

previous answer didn't help me, so i propouse that help for me

以前的回答没有帮助我,所以我建议帮助我

a={1=>1}; a = a.clone; a[2] = 2; a

#1


11  

Ruby since 1.9 is using lazy iteration, thus they forbid to add new keys to the hash you iterating over. As a solution you can easily duplicate hash or convert it to array before doing each.

Ruby从1.9开始使用延迟迭代,因此它们禁止向迭代的哈希添加新键。作为一种解决方案,您可以轻松地复制哈希或将其转换为数组,然后再执

irb(main):001:0> a={1=>1}; a.each {|k,v| a[2] = 2}
RuntimeError: can't add a new key into hash during iteration

irb(main):002:0> a={1=>1}; a.clone.each {|k,v| a[2] = 2}; a
=> {1=>1, 2=>2}

irb(main):003:0> a={1=>1}; a.to_a.each {|k,v| a[2] = 2}; a
=> {1=>1, 2=>2}

#2


9  

You're modifying the hash you're iterating over. You can't do it.

你正在修改你正在迭代的哈希。你不能这样做。

Instead try another approach:

而是尝试另一种方法:

keys = [1,2,3,4]
file_hash = YAML.load_file("testm.yaml")
keys.each{ |key| file_hash[key] = 'key1' }
# => {1 => 'key1', 2 => 'key1', 3 => 'key1', 4 => 'key1'}

#3


0  

previous answer didn't help me, so i propouse that help for me

以前的回答没有帮助我,所以我建议帮助我

a={1=>1}; a = a.clone; a[2] = 2; a