I have such a block of code (this is a working version)
@full_user_schedule[0].attributes.each do |event_name, attending| if attending sessions_hash.each do |id, name| event_name = event_name.sub(name, id.to_s) if event_name.include? name end @user_schedule.merge!(event_name => attending) end end
When I tried to set the event_id variable in the sessions_hash.each statement, I could not force it to be assigned outside this loop (I received a warning for an unknown variable). I would like to know why this is so, and how to get around it.
The following is an example of what was unsuccessful (an event, although the log showed that the variable was set correctly), I did not have access to it outside the loop in which it was created.
@full_user_schedule[0].attributes.each do |event_name, attending| if attending sessions_hash.each do |id, name| event_id = event_name.sub(name, id.to_s) if event_name.include? name end @user_schedule.merge!(event_id => attending) end end
I tried to explicitly return event_id , but exited the loop. What? I do not understand?
source share