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 8828973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:43:42+00:00 2026-06-14T07:43:42+00:00

I am trying to return a subset of a multi-dimensional array, trying to keep

  • 0

I am trying to return a subset of a multi-dimensional array, trying to keep the exact structure of dimensions, but.. something strange is happening… take a look please:

space = [  [ [1],[2],[3] ],  [ [4],[5],[6] ],  [ [70],[8],[9] ]  ]

space_subset = space[(1..2)].collect { |y| y[1] }

=> [[5], [8]] 

Let’s break it down:

space[(1..2)]

=> [  [ [4], [5], [6] ], [ [70], [8], [9] ]  ]

so now I can be sure what I am calling .collect on

in fact:

[  [ [4], [5], [6] ], [ [70], [8], [9] ]  ].collect { |y| y[1] }

=> [[5], [8]]

Then… (for the real question)…

If now space_subset is [[5], [8]]

and I try to modify it like this:

space_subset[1].delete (8)

and as expected I get: => [[5], []]

why does this at the same time modifies the original “space” array from which I extracted the subset array ?

If now I do:

space

=> [[[1], [2], [3]], [[4], [5], [6]], [[70], [], [9]]]

“8” is missing, the same value I deleted from the space_subset

I am looking at ruby Array api docs and from what I am reading my code should work without surprises… but.. still…..

Can you help me figure what I’m doing wrong, or misunderstanding here ?

Thanks to everyone who takes the time to answer

  • 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-14T07:43:44+00:00Added an answer on June 14, 2026 at 7:43 am

    This is a difference between reference and value. In your code, you create a reference to the inner arrays, but you are referencing the same values in both places. You can confirm this by calling Object#object_id on both arrays (as if changing the value via one reference and seeing be modified from the other reference isn’t confirmation enough!).

    space = [  [ [1],[2],[3] ],  [ [4],[5],[6] ],  [ [70],[8],[9] ]  ]
    => [[[1], [2], [3]], [[4], [5], [6]], [[70], [8], [9]]] 
    space[2][1].object_id
    => 70329700053380 
    space_subset = space[(1..2)].collect { |y| y[1] }
    => [[5], [8]] 
    space_subset[1].object_id
    => 70329700053380
    

    Unfortunately, Array#dup and Array#clone only make “shallow” copies of objects, so you have to use a bit of a workaround to get a copy of space to work with. One easy trick to get a deep copy is:

    Marshal.load(Marshal.dump(space))
    

    You can also write a recursive function to take space and manually copy it into a new array.

    And just to prove it:

    space = [  [ [1],[2],[3] ],  [ [4],[5],[6] ],  [ [70],[8],[9] ]  ]
    => [[[1], [2], [3]], [[4], [5], [6]], [[70], [8], [9]]]
    space[2][1].object_id
    => 70329700053380
    space_subset = Marshal.load(Marshal.dump(space))
    => [[[1], [2], [3]], [[4], [5], [6]], [[70], [8], [9]]]
    space_subset = space_subset[(1..2)].collect { |y| y[1] }
    => [[5], [8]]
    space_subset[1].object_id
    => 70329695297500
    space_subset[1].delete(8)
    => 8
    space
    => [[[1], [2], [3]], [[4], [5], [6]], [[70], [8], [9]]]
    space_subset
    => [[5], []]
    

    Hope that helps!

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

Sidebar

Related Questions

I am trying to return the value by using unix_timestamp function but it behaves
I'm trying to write a function that will take an array on input and
I am trying to return a subset of my NSMutableArray (MessageArray) with the following
I'm trying get a subset of keys for each hash in an array. The
I'm trying to return the first created record, and all but the first created
I'm trying to rank a subset of data within a table but I think
Im trying to return the value that a $ajax call returns, from a function
Im trying to return a SimpleQuery list that queries a single table and uses
Am trying to return the sum of each day of a week in mysql
I'm trying to return the most common elements in a list (statistical mode). Unfortunately

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.