Can't add hash key during iteration

Rails 3.0.5
Code that runs fine in 1.8.7 is balking in 1.9.2

I guess 1.9 has fundamentally changed something like this (assuming keys
are symbols):

my_hash.each do |key, value|
my_hash[key.to_s] = value
end

to now require something like this:

my_hash.dup.each do |key, value|
my_hash[key.to_s] = my_hash.delete(key)
end

However, I am getting the error at this simple line (the first line in a
little method to stuff params with some values):

params[:search_form] ||= {}

There is no iteration.

Even changing it to this didn’t help. Didn’t really think it would, but
tried it anyway.

if !params.has_key?(:search_form)
params.store(:search_form, {})
end

Anyone know what the deal is here? Thanks.

On Mar 21, 8:19pm, Greg W. [email protected] wrote:

Rails 3.0.5
Code that runs fine in 1.8.7 is balking in 1.9.2

I guess 1.9 has fundamentally changed something like this (assuming keys
are symbols):

my_hash.each do |key, value|
my_hash[key.to_s] = value
end

Modifying a collection while enumerating over it has always been a
dangerous way to live (do you want stringify_keys (from Active
Support) ?)

params[:search_form] ||= {}

What error?

Fred

Frederick C. wrote in post #988609:

What error?

Sorry, now that I reread my post, it’s not very clear.

I am getting the error “can’t add a new key into hash during iteration”
reported for this line of code:

params[:search_form] ||= {}

which makes no sense at all. The iteration examples in my first post
were just my way of explaining I understood the basic error msg and what
it would normally relate to. However, the actual error msg is not
tracing back to an iteration–just that simple line with params ||=.

Experimenting a little more, it appears it matters where I use that
line of code.

I’m not sure how to distill this down, so here’s the semi-detailed
version…

Two relevant files: a rails controller, and a module file stored in /lib
which gets included in ApplicationController.

#---------------------------------------
class MyController < ApplicationController

include GwControllerExtras

def admin
sasc_initialize(…) # an abstraction for handling a bunch of UI
interaction
end

private

called from within sasc_initialize()

def submit_form_x
end

called from within sasc_initialize()

def prep_view_y
update_sticky_search
end
end

#---------------------------------------

/lib/admin_ui_extras.rb

module GwControllerExtras
def update_sticky_search
params[:search_form] ||= {} # <<<<< FAILS
# more stuff
end
end

OK, so the line that fails is the one shown just above. Now, if I move
that line out of the update_sticky_search method and into the
prep_view_y method just before the call to update_sticky_search, there
is no error. Bizarre!

There’s a couple other places where I can move it in the call chain that
either crashes or doesn’t crash with the “can’t add a new key into hash
during iteration” error. So, somehow, location matters.

There’s something specific to 192 here, because update_sticky_search
works fine in 186 and 187.

I hope that’s clearer.

– gw

On Mar 21, 9:38pm, Greg W. [email protected] wrote:

OK, so the line that fails is the one shown just above. Now, if I move
that line out of the update_sticky_search method and into the
prep_view_y method just before the call to update_sticky_search, there
is no error. Bizarre!

So is update_sticky_search called from someplace that (among other
things) is iterating over params?

Fred

I am getting the error “can’t add a new key into hash during iteration”
reported for this line of code:
params[:search_form] ||= {}

After much tinkering, I found a clean enough work around to live with
it, but still no closer to understanding why the error was happening.

– gw

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs