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

  • SEARCH
  • Home
  • 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 6560973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:31:48+00:00 2026-05-25T13:31:48+00:00

I was debugging an issue in an RoR application and came across with below

  • 0

I was debugging an issue in an RoR application and came across with below code (where ‘map’ is a two dimensional array of integer). The code attempts to duplicate and append the last element of each sub-array:

map.each { |x| x << x[-1] }

Before this line of code,

    (rdb:29) p map
    [[1, 2, 1, 3, 4], [1, 2, 2, 3, 4], [1, 2, 2, 3, 4], [1, 2, 2, 3, 4],
    [1, 2, 2, 3, 4], [1, 2, 2, 3, 4]]
    (rdb:29) p map.class
    Array
    (rdb:29) p map.first.class
    Array
    (rdb:29) p map.last.class
    Array

After:

    (rdb:29) p map
    [[1, 2, 1, 3, 4, 4], [1, 2, 2, 3, 4, 4], [1, 2, 2, 3, 4, 4],
    [1, 2, 2, 3, 4, 4], [1, 2, 2, 3, 4, 4, 4], [1, 2, 2, 3, 4, 4, 4]]

The problem here is that the last 2 sub-arrays were appended with two integers instead of one, while it is correct for the first 4 sub-arrays. I changed the code to use Array.map instead, it was then working properly:

map = map.map { |x| x + [x[-1]] }

To sum up:
I know the iterating element in the Array.each block is not supposed to be changed. But why is it giving unpredictable results when doing so? The code actually works most of the time, the issue was just seen sometimes. Was it a bug in Ruby or RoR?

  • 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-05-25T13:31:49+00:00Added an answer on May 25, 2026 at 1:31 pm

    Modifying the sub-array within the each block isn’t your problem, there’s nothing wrong with that at all. Your problem is that your outer array, map, sometimes contains multiple references to the same sub-array object.

    Consider this:

    >> x = [[1, 2, 1, 3, 4], [1, 2, 2, 3, 4], [1, 2, 2, 3, 4]]
    >> x.each { |a| a << a[-1] }
    => [[1, 2, 1, 3, 4, 4], [1, 2, 2, 3, 4, 4], [1, 2, 2, 3, 4, 4]]
    

    The results will be the same every single time. But if you have this:

    >> gotcha = [1, 2, 1, 3, 4]
    >> x = [[1, 2, 1, 3, 4], gotcha, gotcha]
    >> x.each { |a| a << a[-1] }
    => [[1, 2, 1, 3, 4, 4], [1, 2, 1, 3, 4, 4, 4], [1, 2, 1, 3, 4, 4, 4]]
    

    Then you will get the extra trailing elements that you were seeing (every single time) because gotcha gets modified twice. The x in the second case will puts the same as the x in the first case but they are not the same.

    Your Array#map approach always works because this:

    x + [x[-1]]
    

    essentially copies x and then appends x[-1] to that copy, it never modifies x at all so the gotcha behavior from above won’t happen.

    You can’t get away from pointers, not even when they’re called references.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm debugging an issue on OS X that only occurs when the application is
Debugging some finance-related SQL code found a strange issue with numeric(24,8) mathematics precision. Running
I am debugging an issue in a legacy PHP application where a certain column
Hello I have an issue with debugging code of my UserControls. I am adding
I'm having a tricky debugging issue, perhaps due to my lack of understanding about
While debugging an issue with our system, I have discovered a thread contention that
I was working on debugging an issue today related to mixing mod_proxy and mod_rewrite
Debugging with gdb, any c++ code that uses STL/boost is still a nightmare. Anyone
Debugging a Firefox addon is a slow process: (1) edit source code in a
I just spent a very long time debugging an issue in python, using the

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.