20

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

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

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

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Cristian M
  • 357
  • 1
  • 3
  • 18

4 Answers4

22

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.

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}
catpnosis
  • 546
  • 4
  • 13
  • This issue came up for me in a project that included a Ruby 1.8 to 1.9 upgrade, and this solution worked for me. – Paul Fioravanti Jan 30 '15 at 05:34
  • This worked for me. I was wondering why my 2.2.2 script worked and it didn't work on previous versions. Thanks. For reference, use a.to_a.each to make it easy on you – Hyra Power Dec 03 '15 at 05:53
10

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'}
mie
  • 721
  • 5
  • 16
  • Thank you very much. This helps quite a lot, but now I am facing another issue. Fore each new key inserted, I need to add string values. The problem is that yaml creates the new keys as strings. Is there a way to tell yaml not to write the keys as strings? – Cristian M Aug 10 '14 at 08:15
2

I had the same error at

<%= stylesheet_link_tag 'application', 'data-turbolinks-track' => 'reload', media: 'all' %>

So I rewrite my code with:

<% begin %>
  <%= stylesheet_link_tag 'application', 'data-turbolinks-track' => 'reload', media: 'all' %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => 'reload' %>
<% rescue %>
  <script>
    window.location.replace "#{user_session_url()}"
  </script>
<% end %>

I do not know what is causing this issue, but this workaround works for me. I tested it with

ab -n 100 -c 10 -w http://localhost:3000/es

And I did not got any error messages

OfficeYA
  • 737
  • 8
  • 15
0

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

a={1=>1}; a = a.clone; a[2] = 2; a
FelixSFD
  • 6,052
  • 10
  • 43
  • 117