Cloning an array with its content

Redouane Red

I want to make a copy of an array, to modify the copy in-place, without affecting the original one. This code fails

a = [
  '462664',
  '669722',
  '297288',
  '796928',
  '584497',
  '357431'
]
b = a.clone
b.object_id == a.object_id # => false
a[1][2] = 'X'
a[1] #66X722
b[1] #66X722

The copy should be different than the object. Why does it act like if it were just a reference?

jazzytomato

You need to do a deep copy of your array.

Here is the way to do it

Marshal.load(Marshal.dump(a))

This is because you are cloning the array but not the elements inside. So the array object is different but the elements it contains are the same instances. You could, for example, also do a.each{|e| b << e.dup} for your case

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery Cloning Altered Content

From Dev

How to sort an Array by the content of its documents in MongoDB

From Dev

PHP - Split an array into chunks based on its content

From Java

Cloning an array in Javascript/Typescript

From Dev

JavaScript array cloning

From Dev

How to modify the content of an array according to its content ordering efficiently?

From Dev

Cloning object based on content in PHP

From Dev

Split numpy array into similar array based on its content

From Dev

Cloning an object and its inside arrays in Java DIY

From Dev

Jquery: cloning a table and its first row

From Dev

Trouble deep cloning an array object

From Dev

Extending a numpy array up to a specific range with its own content

From Dev

How do I pass an array to a function in Rust and change its content?

From Dev

Backbone remove and new view does not reset its attribute array content

From Dev

PHP array, selecting a printing a specific element and its content

From Dev

Why my array suddenly lose all its content

From Dev

targeting an element by its content and the content of its ancestors

From Dev

Cloning an object with Knockout that has an observable array

From Dev

How to get sub array without cloning

From Dev

TextView will not center its content

From Dev

delete a folder and its content

From Dev

if condition changed by its content

From Dev

Relativelayout not wrapping its content

From Dev

Summernote will not edit its content

From Dev

TextView will not center its content

From Dev

Get cell by its content

From Dev

Aggregate vector by its content

From Dev

How would you simplify this ruby method that splits an array based on its content?

From Dev

sort an array and loop its content by desc date after selecting an option in ReactJS

Related Related

  1. 1

    jQuery Cloning Altered Content

  2. 2

    How to sort an Array by the content of its documents in MongoDB

  3. 3

    PHP - Split an array into chunks based on its content

  4. 4

    Cloning an array in Javascript/Typescript

  5. 5

    JavaScript array cloning

  6. 6

    How to modify the content of an array according to its content ordering efficiently?

  7. 7

    Cloning object based on content in PHP

  8. 8

    Split numpy array into similar array based on its content

  9. 9

    Cloning an object and its inside arrays in Java DIY

  10. 10

    Jquery: cloning a table and its first row

  11. 11

    Trouble deep cloning an array object

  12. 12

    Extending a numpy array up to a specific range with its own content

  13. 13

    How do I pass an array to a function in Rust and change its content?

  14. 14

    Backbone remove and new view does not reset its attribute array content

  15. 15

    PHP array, selecting a printing a specific element and its content

  16. 16

    Why my array suddenly lose all its content

  17. 17

    targeting an element by its content and the content of its ancestors

  18. 18

    Cloning an object with Knockout that has an observable array

  19. 19

    How to get sub array without cloning

  20. 20

    TextView will not center its content

  21. 21

    delete a folder and its content

  22. 22

    if condition changed by its content

  23. 23

    Relativelayout not wrapping its content

  24. 24

    Summernote will not edit its content

  25. 25

    TextView will not center its content

  26. 26

    Get cell by its content

  27. 27

    Aggregate vector by its content

  28. 28

    How would you simplify this ruby method that splits an array based on its content?

  29. 29

    sort an array and loop its content by desc date after selecting an option in ReactJS

HotTag

Archive