Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8727363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:25:24+00:00 2026-06-13T08:25:24+00:00

The code below loops through an array factor_list and checks if each of them

  • 0

The code below loops through an array factor_list and checks if each of them contains a specified variable. If so, remove them from the array, multiply them together, and sum out the final result with respect to the variable. After all the operations, add the factor back to the array.

temp_factor = nil
factor_list.each{|factor|
        if factor._variables.include?(variable)
            if temp_factor == nil
                temp_factor = factor
            else
                temp_factor = multiply(temp_factor, factor)
            end
            factor_list.delete(factor)
        end
    }
temp_factor = sumOut(temp_factor, variable)
factor_list << temp_factor

The problem is, temp_factor is always nil in every iteration, even if it has been set in a previous loop. I thought the main problem was because of the deletion in the array, so I removed the deletion for testing, and that solved the problem (but my array is full of trash of course). So I came to the conclusion that my temp_factor was a shallow copy of the object, and so its referencing object is gone with the original one. I then tried doing a deep copy using the marshal trick, but it didn’t help.

That was all I have got, as I was unable to solve the problem. Can anyone help me identify the mechanism behind all these myths?

It’s cool that you guys give very nice advice about rewriting the code to avoid problems, and they really help! But I’m still wondering what caused the above problem? It would be nice if I can learn that bit of information!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:25:24+00:00Added an answer on June 13, 2026 at 8:25 am

    College years with C++ and STL had taught me hard: never ever modify a collection that you’re iterating at the moment. So, instead of deleting items in place, why don’t you try to build a new array?

    temp_factor = nil
    new_factors = factor_list.map do |factor|
      if factor._variables.include?(variable)
        if temp_factor == nil
          temp_factor = factor
        else
          temp_factor = multiply(temp_factor, factor)
        end
        nil
      else
        factor
      end
    end.compact
    
    temp_factor = sumOut(temp_factor, variable)
    factor_list = new_factors + [temp_factor]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the below code I'm trying to loop through each child node and append
Can someone please point out whats going wrong here: The code below loops through
The code below shows me (I think) that the for each loop is about
I have some image processing code that loops through 2 multi-dimensional byte arrays (of
I have the below code to loop through and run an external file. I
I have the code below that requires me to convert a character array to
I have created an array of data, from which I can loop through specific
I have the code below and I want to loop this every 5 seconds.
In the code below is the initial first loop needed when creating a 2D
I have the code below. It looks long and complicated and now I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.