Recursive deep copy


#1

hello,

It’s a little bit hard question:
i’ve a problem when working on arrays. i’m doing a deep copy of an
array (a find request result) by doing it :
@array = new.Array(@original_array)
it just copies the first level, under levels arrays and hashes are not
include in deep copy!

some can help?


#2

What about this :

new_array = old_array

:wink:


#3

Hi –

On Thu, 10 May 2007, Jean-Sébastien wrote:

some can help?
The most common idiom for this is:

copy = Marshal.load(Marshal.dump(@array))

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)


#4

Hi –

On Thu, 10 May 2007, Titou T. wrote:

What about this :

new_array = old_array

He wants a deep copy, and that’s neither deep nor a copy. It’s just a
new reference to the same array:

array = [1,2,3]
new_array = array
new_array.push(4)
p array # [1, 2, 3, 4]

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)


#5

thank you guys, “@array =
Marshal::load(Marshal.dump(@original_array))” works perfectly